Python中MRO

MRO(方法解析顺序)

当有多重继承时,基于“从左到右,深度优先原则”:

class CommonBase():
    def Method(self):
        print('CommonBase')

class Base1(CommonBase):
    pass

class Base2(CommonBase):
    def Method(self):
        print('Base2')

class MyClass(Base1,Base2):
    pass


MyClass().Method()

# 执行结果:CommonBase

当然如上算法时在python2中旧式类中(不继承object)存在这样的算法,当然在python3中定义类的时候已经显示的继承了object(不管你是否写都默认地继承了object)。当然也提供了一个新的MRO算法,在python3中运行如上代码结果为:Base2

python super()调用多重继承函数问题

https://blog.csdn.net/dl_chenbo/article/details/80602113

如上博文也阐明了关于多重继承MRO问题,初次接触肯定有违你的初衷。 明白MRO的算法之后,还要考虑一个问题就是不同种类的参数问题:

class CommonBase:
    def __init__(self):
        print('CommonBase')
        super().__init__()

class Base1(CommonBase):
    def __init__(self):
        print('Base1')
        super().__init__()

class Base2(CommonBase):
    def __init__(self,arg):
        print('Base2')
        super().__init__()

class MyClass(Base1,Base2):
    def __init__(self,arg):
        print('MyClass')
        super().__init__(arg)

MyClass(10)

# 报错如下:

Traceback (most recent call last):
  File "E:/QMYG.BLOG/awesome-py3-webapp/test2.py", line 47, in <module>
    MyClass(10)
  File "E:/QMYG.BLOG/awesome-py3-webapp/test2.py", line 45, in __init__
    super().__init__(arg)
TypeError: __init__() takes 1 positional argument but 2 were given

解决如上问题,可以使用arg,*kw接收参数,但是这种设计方式不推荐,任何参数都可以传入使得代码比较脆弱。

最佳实践

  • 应该避免多重继承
  • super的使用必须一致
  • 显示地继承object
  • 调用父类时必须查看类的层次结构,通过mro

python私有属性

使用双下划线为前缀,解析器会把该属性重命名,使用组合名称依然可以访问该属性。真正定义私有属性的时候约定使用 _ 前缀,虽然同样可以访问,但不会调用任何名称修饰的算法。

原文地址:https://www.cnblogs.com/tianboblog/p/9690763.html