一直困惑于python继承类的程序执行顺序,看了这段代码后理解得到了加深,跟大家分享一下学习心得。
单继承
class FooParent(object):
def __init__(self):
self.parent = 'I\'m the parent.'
print ('Parent')
def bar(self,message):
print ("%s from Parent" % message)
class FooChild(FooParent):
def __init__(self):
# super(FooChild,self) 首先找到 FooChild 的父类(就是类 FooParent),然后把类 FooChild 的对象转换为类 FooParent 的对象
super(FooChild,self).__init__()
print ('Child')
def bar(self,message):
super(FooChild, self).bar(message)
print ('Child bar fuction')
print (self.parent)
if __name__ == '__main__':
fooChild = FooChild()
fooChild.bar('HelloWorld')
输出结果如下。
Parent
Child
HelloWorld from Parent
Child bar fuction
I’m the parent.
执行顺序分析:
- super()父类的__init__
- 子类的__init__
- super()继承父类的bar方法
- 子类的bar方法
作用:super()避免了基类的显式调用,即super(FooChild,self).init()与FooParent.init()等价
说明__init__中的super()会继承父类中的self.parent,将上述代码修改如下
class FooParent(object):
def __init__(self):
# self.parent = 'I\'m the parent.'
print ('Parent')
def bar(self,message):
print ("%s from Parent" % message)
class FooChild(FooParent):
def __init__(self):
# super(FooChild,self) 首先找到 FooChild 的父类(就是类 FooParent),然后把类 FooChild 的对象转换为类 FooParent 的对象
# super(FooChild,self).__init__()
print ('Child')
def bar(self,message):
super(FooChild, self).bar(message)
print ('Child bar fuction')
# print (self.parent)
if __name__ == '__main__':
fooChild = FooChild()
fooChild.bar('HelloWorld')
Child
HelloWorld from Parent
Child bar fuction
可见,并没有执行父类__init__中的代码
同理注释掉第二个super()结果如下
class FooParent(object):
def __init__(self):
# self.parent = 'I\'m the parent.'
print ('Parent')
def bar(self,message):
print ("%s from Parent" % message)
class FooChild(FooParent):
def __init__(self):
# super(FooChild,self) 首先找到 FooChild 的父类(就是类 FooParent),然后把类 FooChild 的对象转换为类 FooParent 的对象
# super(FooChild,self).__init__()
print ('Child')
def bar(self,message):
# super(FooChild, self).bar(message)
print ('Child bar fuction')
# print (self.parent)
if __name__ == '__main__':
fooChild = FooChild()
fooChild.bar('HelloWorld')
Child
Child bar fuction
多继承
对于多层继承关系,python会通过创建一个MRO(Method Resolution Order)列表,它代表了类继承的顺序。使用super()后,程序查看MRO列表,自动进入上一级的类。
class Base(object):
def __init__(self):
print ("enter Base")
print ("leave Base")
class A(Base):
def __init__(self):
print ("enter A")
super(A, self).__init__()
print ("leave A")
class B(Base):
def __init__(self):
print ("enter B")
super(B, self).__init__()
print ("leave B")
class C(A, B):
def __init__(self):
print ("enter C")
super(C, self).__init__()
print ("leave C")
if __name__ == '__main__':
C()
print(C.mro())
enter C
enter A
enter B
enter Base
leave Base
leave B
leave A
leave C
[<class ‘main.C’>, <class ‘main.A’>, <class ‘main.B’>, <class ‘main.Base’>, <class ‘object’>]
注意class C(B, A)与class C(A,B)是有差异,调用上级类的现后顺序存在差别
参考
https://www.runoob.com/python/python-func-super.html
https://www.jianshu.com/p/8ddb595628d1