如何确定类之间的关系?

时间:2016-08-10 09:09:15

标签: python

我有两个班级:

class A(object):
    """Instance of this class must by only one"""

    def some_function(self):
         print "A function"

    [... other functions ...]


class B(object):
    """Many instances of this class"""

    [... functions ...]

我想从B类只初始化一个A类对象,我写这个:

class B(object):
    a_instance = A()
    b_instances = []

    def __init__(self, *args, **kwargs):
        B.a_instance.some_function()
        B.b_instances.append(self)
        super(B, self).__init__(*args, **kwargs)

    def update(self):
        print "Update instance"

    @classmethod
    def super_function(self):
        print "My super B function"
        for instance in B.b_instances:
             instance.update()

这是对的吗?

第二个问题:如何从A类实例中调用“super_function”?

0 个答案:

没有答案