python 继承类 super() 程序执行顺序

一直困惑于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.

执行顺序分析:

  1. super()父类的__init__
  2. 子类的__init__
  3. super()继承父类的bar方法
  4. 子类的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

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
(metaclass) 是 Python 中的一个高级特性,它是用于创建的“”。可以将元视为的模板,元创建创建对象。 在 Python 中,我们通常使用 `class` 关键字来定义一个,但实际上,`class` 关键字本身也是一个元创建的。我们可以通过自定义元来改变的创建方式,从而实现一些高级的功能,例如: - 控制的创建过程:元可以拦截的创建过程,可以在被创建之或之后执行一些额外的操作,例如修改的属性、方法等。 - 实现自定义的属性或方法:元可以添加一些自定义的属性或方法到中,这些属性或方法可以被的实例所继承和使用。 - 实现单例模式:元可以控制的创建过程,从而实现单例模式,即在应用程序中只创建一个的实例。 元的实现方式是通过定义一个继承自 `type` 的,并通过 `__new__` 方法来控制的创建过程。例如: ```python class MyMeta(type): def __new__(cls, name, bases, attrs): # 在创建执行一些额外的操作 attrs['my_attr'] = 'my value' return super().__new__(cls, name, bases, attrs) class MyClass(metaclass=MyMeta): pass print(MyClass.my_attr) # 输出:my value ``` 上面的代码定义了一个自定义元 `MyMeta`,它在创建时会添加一个自定义属性 `my_attr` 到中。然后创建了一个名为 `MyClass` 的,并将 `MyMeta` 指定为它的元。最后输出了 `MyClass.my_attr`,结果为 `my value`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值