class A(object):
def __init__(self, a, b, c):
#super(A, self).__init__()
super(self.__class__, self).__init__()
class B(A):
def __init__(self, b, c):
print super(B, self)
print super(self.__class__, self)
#super(B, self).__init__(1, b, c)
super(self.__class__, self).__init__(1, b, c)
class C(B):
def __init__(self, c):
#super(C, self).__init__(2, c)
super(self.__class__, self).__init__(2, c)
C(3)
在上面的代码中,注释掉的__init__调用似乎是进行超类初始化的普遍接受的“智能”方式.但是,如果类层次结构可能会发生变化,我一直在使用未注释的表单,直到最近.
看来,在上面层次结构中对B的超级构造函数的调用中,再次调用B .__ init__,self .__ class__实际上是C,而不是我一直假设的B.
在Python-2.x中是否有某些方法可以在调用超级构造函数时保持正确的MRO(关于以正确的顺序初始化所有父类)而不命名当前类(超级中的B in(B,self) .__ init __(1,b,c))?