转自 :http://www.weidianyuedu.com/content/5411691841425.html
super()可以避免重复调用
如果childA基础Base, childB继承childA和Base,如果childB需要调用Base的__init__()方法时,就会导致__init__()被执行两次:
class Base(object):
def init(self):
print ‘Base create’
class childA(Base):
def init(self):
print 'enter A ’
Base.init(self)
print ‘leave A’
class childB(childA, Base):
def init(self):
childA.init(self)
Base.init(self)
b = childB()
Base的__init__()方法被执行了两次
enter A
Base create
leave A
Base create
使用super()是可避免重复调用
class Base(object):
def init(self):
print ‘Base create’
class childA(Base):
def init(self):
print 'enter A ’
super(childA, self).init()
print ‘leave A’
class childB(childA, Base):
def init(self):
super(childB, self).init()
b = childB()
print b.class.mro()
enter A
Base create
leave A
[, , , ]