良许Linux教程网 干货合集 详解 Python中的super()

详解 Python中的super()

super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO)、重复调用(钻石继承)等种种问题。这篇文章主要给大家介绍了关于Python中super()方法的相关

详解 Python中的super()

说到 super, 大家可能觉得很简单呀,不就是用来调用父类方法的嘛。如果真的这么简单的话也就不会有这篇文章了,且听我细细道来。

约定

在开始之前我们来约定一下本文所使用的 Python 版本。默认用的是 Python 3,也就是说:本文所定义的类都是新式类。如果你用到是 Python 2 的话,记得继承 object:

# 默认, Python 3
class A:
   pass

# Python 2
class A(object):
   pass

Python 3 和 Python 2 的另一个区别是: Python 3 可以使用直接使用 super().xxx 代替 super(Class, self).xxx :

# 默认,Python 3
class B(A):
   def add(self, x):
       super().add(x)

# Python 2
class B(A):
   def add(self, x):
       super(B, self).add(x)

所以,你如果用的是 Python 2 的话,记得将本文的 super() 替换为 suepr(Class, self) 。

如果还有其他不兼容 Python 2 的情况,我会在文中注明的。

单继承

在单继承中 super 就像大家所想的那样,主要是用来调用父类的方法的。

class A:
   def __init__(self):
       self.n = 2

   def add(self, m):
       print('self is {0} @A.add'.format(self))
       self.n += m


class B(A):
   def __init__(self):
       self.n = 3

   def add(self, m):
       print('self is {0} @B.add'.format(self))
       super().add(m)
       self.n += 3

你觉得执行下面代码后, b.n 的值是多少呢?

b = B()
b.add(2)
print(b.n)

执行结果如下:

self is <__main__.b> @B.add
self is <__main__.b> @A.add
8

这个结果说明了两个问题:

  1. super().add(m) 确实调用了父类 A 的 add 方法。
  2. super().add(m) 调用父类方法 def add(self, m) 时, 此时父类中 self 并不是父类的实例而是子类的实例, 所以 b.add(2) 之后的结果是 5 而不是 4 。

不知道这个结果是否和你想到一样呢?下面我们来看一个多继承的例子。

多继承

这次我们再定义一个 class C,一个 class D:

class C(A):
   def __init__(self):
       self.n = 4

   def add(self, m):
       print('self is {0} @C.add'.format(self))
       super().add(m)
       self.n += 4


class D(B, C):
   def __init__(self):
       self.n = 5

   def add(self, m):
       print('self is {0} @D.add'.format(self))
       super().add(m)
       self.n += 5

下面的代码又输出啥呢?

d = D()
d.add(2)
print(d.n)

这次的输出如下:

self is <__main__.d> @D.add
self is <__main__.d> @B.add
self is <__main__.d> @C.add
self is <__main__.d> @A.add
19

你说对了吗?你可能会认为上面代码的输出类似:

self is <__main__.d> @D.add
self is <__main__.d> @B.add
self is <__main__.d> @A.add
15

为什么会跟预期的不一样呢?下面我们将一起来看看 super 的奥秘。

super 是个类

当我们调用 super() 的时候,实际上是实例化了一个 super 类。你没看错, super 是个类,既不是关键字也不是函数等其他数据结构:

>>> class A: pass
...
>>> s = super(A)
>>> type(s)

>>>

在大多数情况下, super 包含了两个非常重要的信息: 一个 MRO 以及 MRO 中的一个类。当以如下方式调用 super 时:

super(a_type, obj)

MRO 指的是 type(obj) 的 MRO, MRO 中的那个类就是 a_type , 同时 isinstance(obj, a_type) == True 。

当这样调用时:

super(type1, type2)

MRO 指的是 type2 的 MRO, MRO 中的那个类就是 type1 ,同时 issubclass(type2, type1) == True 。

那么, super() 实际上做了啥呢?简单来说就是:提供一个 MRO 以及一个 MRO 中的类 C , super() 将返回一个从 MRO 中 C 之后的类中查找方法的对象。

也就是说,查找方式时不是像常规方法一样从所有的 MRO 类中查找,而是从 MRO 的 tail 中查找。

举个例子, 有个 MRO:

[A, B, C, D, E, object]

下面的调用:

super(C, A).foo()

super 只会从 C 之后查找,即: 只会在 D 或 E 或 object 中查找 foo 方法。

多继承中 super 的工作方式

再回到前面的

d = D()
d.add(2)
print(d.n)

现在你可能已经有点眉目,为什么输出会是

self is <__main__.d> @D.add
self is <__main__.d> @B.add
self is <__main__.d> @C.add
self is <__main__.d> @A.add
19

了吧 😉

下面我们来具体分析一下:

D 的 MRO 是: [D, B, C, A, object] 。 备注: 可以通过 D.mro() (Python 2 使用 D.mro ) 来查看 D 的 MRO 信息)

详细的代码分析如下:

class A:
   def __init__(self):
       self.n = 2

   def add(self, m):
       # 第四步
       # 来自 D.add 中的 super
       # self == d, self.n == d.n == 5
       print('self is {0} @A.add'.format(self))
       self.n += m
       # d.n == 7


class B(A):
   def __init__(self):
       self.n = 3

   def add(self, m):
       # 第二步
       # 来自 D.add 中的 super
       # self == d, self.n == d.n == 5
       print('self is {0} @B.add'.format(self))
       # 等价于 suepr(B, self).add(m)
       # self 的 MRO 是 [D, B, C, A, object]
       # 从 B 之后的 [C, A, object] 中查找 add 方法
       super().add(m)

       # 第六步
       # d.n = 11
       self.n += 3
       # d.n = 14

class C(A):
   def __init__(self):
       self.n = 4

   def add(self, m):
       # 第三步
       # 来自 B.add 中的 super
       # self == d, self.n == d.n == 5
       print('self is {0} @C.add'.format(self))
       # 等价于 suepr(C, self).add(m)
       # self 的 MRO 是 [D, B, C, A, object]
       # 从 C 之后的 [A, object] 中查找 add 方法
       super().add(m)

       # 第五步
       # d.n = 7
       self.n += 4
       # d.n = 11


class D(B, C):
   def __init__(self):
       self.n = 5

   def add(self, m):
       # 第一步
       print('self is {0} @D.add'.format(self))
       # 等价于 super(D, self).add(m)
       # self 的 MRO 是 [D, B, C, A, object]
       # 从 D 之后的 [B, C, A, object] 中查找 add 方法
       super().add(m)

       # 第七步
       # d.n = 14
       self.n += 5
       # self.n = 19

d = D()
d.add(2)
print(d.n)

调用过程图如下:

D.mro() == [D, B, C, A, object]
d = D()
d.n == 5
d.add(2)

class D(B, C):          class B(A):            class C(A):             class A:
   def add(self, m):       def add(self, m):      def add(self, m):       def add(self, m):
       super().add(m)  1.--->  super().add(m) 2.--->  super().add(m)  3.--->  self.n += m
       self.n += 5   
Python中的super()用法Python中的super()用法

以上就是良许教程网为各位朋友分享的Linu系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你 !

137e00002230ad9f26e78-265x300
本文由 良许Linux教程网 发布,可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
良许

作者: 良许

良许,世界500强企业Linux开发工程师,公众号【良许Linux】的作者,全网拥有超30W粉丝。个人标签:创业者,CSDN学院讲师,副业达人,流量玩家,摄影爱好者。
上一篇
下一篇

发表评论

联系我们

联系我们

公众号:良许Linux

在线咨询: QQ交谈

邮箱: yychuyu@163.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部