Python - 面向对象编程 - 使用 super() 的一些注意事项

super() 详解

https://www.cnblogs.com/poloyy/p/15223443.html

 

多继承中使用 super()

class A:
    def test(self):
        print("AAA")


class B:
    def test(self):
        print("BBB")


class C(A, B):
    def test(self):
        print("CCC")
        super().test()

c = C()
c.test()


# 输出结果
CCC
AAA

根据 MRO,super() 很明显调用的是 A 类的 test() 方法

假设想调用 B 类的 test() 方法,要怎么做呢?

 

方式

class C(A, B):
    def test(self):
        print("CCC")
        super().test()
        B.test(self)


# 输出结果
CCC
AAA
BBB

通过 父类名.方法名() 即可,但这样和 super() 混用,不是一个好编码习惯,具体看下面

 

混用super() 和 显示类调用

class A:
    def __init__(self):
        print("A", end=" ")
        super().__init__()


class B:
    def __init__(self):
        print("B", end=" ")
        super().__init__()


class C(A, B):
    def __init__(self):
        print("C", end=" ")
        A.__init__(self)
        B.__init__(self)


print("MRO:", [x.__name__ for x in C.__mro__])
C()



# 输出结果
MRO: ['C', 'A', 'B', 'object']
C A B B 

B 类的 __init__() 方法被调用了两次

 

为什么呢?

  • C 类里面又通过 B.__init__() 显式的调用了一次
  • 从 MRO 可以看到,A 类后面跟的是 B 类,所以 A 类的 super() 会调用 B 类
  • 一共调用了两次

 

如何避免

  • 在多继承场景中,super() 的使用必须一致,即在类的层次结构中,要么全部使用 super(),要么全不用!坚决不混用
  • 继承父类时应该查看类的层次结构,就是使用类的 __mro__  属性,或者 mro() 方法查看相关类的 MRO

  

posted @ 2021-09-06 23:25  小菠萝测试笔记  阅读(1039)  评论(0编辑  收藏  举报