在以下示例中,如果我将CL2中的super替换为“in cl2 test cl5”中的self.test(),则会得到相同的输出。超级如何做出任何改变。在python中是否超级冗余?
class CL1(object):
def test(self):
print "test cl1"
class CL2(CL1):
def abc(self):
print "in cl2"
super(CL2,self).test()
#self.test()
class CL3(CL1):
def test(self):
print "test cl3"
class CL5(CL1):
def test(self):
print "test cl5"
class CL4(CL2,CL5,CL3):
def add(self):
print 'cl4'
def main()
o=CL4()
o.abc()
if __name__ == "__main__":
main()
有趣的问题。当对“对象”以外的任何其他对象进行子类化时,这是绝对必要的。 –
'super'被误称;它不一定是指使用它的类的(单个)静态定义的父类。 – chepner