|
1 | | -# super() 函数 |
| 1 | +# super() 的使用 |
| 2 | + |
| 3 | +在类的继承中,如果重定义某个方法,该方法会覆盖父类的同名方法,但有时,我们希望能同时实现父类的功能,这时,我们就需要调用父类的方法了,可通过使用 `super` 来实现,比如: |
| 4 | + |
| 5 | +```python |
| 6 | +class Animal(object): |
| 7 | + def __init__(self, name): |
| 8 | + self.name = name |
| 9 | + def greet(self): |
| 10 | + print 'Hello, I am %s.' % self.name |
| 11 | + |
| 12 | +class Dog(Animal): |
| 13 | + def greet(self): |
| 14 | + super(Dog, self).greet() # Python3 可使用 super().greet() |
| 15 | + print 'WangWang...' |
| 16 | +``` |
| 17 | + |
| 18 | +在上面,Animal 是父类,Dog 是子类,我们在 Dog 类重定义了 `greet` 方法,为了能同时实现父类的功能,我们又调用了父类的方法,看下面的使用: |
| 19 | + |
| 20 | +```python |
| 21 | +>>> dog = Dog('dog') |
| 22 | +>>> dog.greet() |
| 23 | +Hello, I am dog. |
| 24 | +WangWang.. |
| 25 | +``` |
| 26 | + |
| 27 | +`super` 的一个最常见用法可以说是在子类中调用父类的初始化方法了,比如: |
| 28 | + |
| 29 | +```python |
| 30 | +class Base(object): |
| 31 | + def __init__(self, a, b): |
| 32 | + self.a = a |
| 33 | + self.b = b |
| 34 | + |
| 35 | +class A(Base): |
| 36 | + def __init__(self, a, b, c): |
| 37 | + super(A, self).__init__(a, b) # Python3 可使用 super().__init__(a, b) |
| 38 | + self.c = c |
| 39 | +``` |
| 40 | + |
| 41 | +# 深入 super() |
| 42 | + |
| 43 | +看了上面的使用,你可能会觉得 `super` 的使用很简单,无非就是获取了父类,并调用父类的方法。其实,在上面的情况下,super 获得的类刚好是父类,但在其他情况就不一定了,**super 其实和父类没有实质性的关联**。 |
| 44 | + |
| 45 | +让我们看一个稍微复杂的例子,涉及到多重继承,代码如下: |
| 46 | + |
| 47 | +```python |
| 48 | +class Base(object): |
| 49 | + def __init__(self): |
| 50 | + print "enter Base" |
| 51 | + print "leave Base" |
| 52 | + |
| 53 | +class A(Base): |
| 54 | + def __init__(self): |
| 55 | + print "enter A" |
| 56 | + super(A, self).__init__() |
| 57 | + print "leave A" |
| 58 | + |
| 59 | +class B(Base): |
| 60 | + def __init__(self): |
| 61 | + print "enter B" |
| 62 | + super(B, self).__init__() |
| 63 | + print "leave B" |
| 64 | + |
| 65 | +class C(A, B): |
| 66 | + def __init__(self): |
| 67 | + print "enter C" |
| 68 | + super(C, self).__init__() |
| 69 | + print "leave C" |
| 70 | +``` |
| 71 | + |
| 72 | +其中,Base 是父类,A, B 继承自 Base, C 继承自 A, B,它们的继承关系是一个典型的『菱形继承』,如下: |
| 73 | + |
| 74 | +``` |
| 75 | + Base |
| 76 | + / \ |
| 77 | + / \ |
| 78 | + A B |
| 79 | + \ / |
| 80 | + \ / |
| 81 | + C |
| 82 | +``` |
| 83 | + |
| 84 | +现在,让我们看一下使用: |
| 85 | + |
| 86 | +```python |
| 87 | +>>> c = C() |
| 88 | +enter C |
| 89 | +enter A |
| 90 | +enter B |
| 91 | +enter Base |
| 92 | +leave Base |
| 93 | +leave B |
| 94 | +leave A |
| 95 | +leave C |
| 96 | +``` |
| 97 | + |
| 98 | +如果你认为 `super` 代表『调用父类的方法』,那你很可能会疑惑为什么 enter A 的下一句不是 enter Base 而是 enter B。原因是,**`super` 和父类没有实质性的关联**,现在让我们搞清 `super` 是怎么运作的。 |
| 99 | + |
| 100 | +## MRO 列表 |
| 101 | + |
| 102 | +事实上,对于你定义的每一个类,Python 会计算出一个**方法解析顺序(Method Resolution Order, MRO)列表**,**它代表了类继承的顺序**,我们可以使用下面的方式获得某个类的 MRO 列表: |
| 103 | + |
| 104 | +```python |
| 105 | +>>> C.mro() # or C.__mro__ or C().__class__.mro() |
| 106 | +[__main__.C, __main__.A, __main__.B, __main__.Base, object] |
| 107 | +``` |
| 108 | + |
| 109 | +那这个 MRO 列表的顺序是怎么定的呢,它是通过一个 [C3 线性化算法](https://www.python.org/download/releases/2.3/mro/)来实现的,这里我们就不去深究这个算法了,感兴趣的读者可以自己去了解一下,总的来说,一个类的 MRO 列表就是合并所有父类的 MRO 列表,并遵循以下三条原则: |
| 110 | + |
| 111 | +- 子类永远在父类前面 |
| 112 | +- 如果有多个父类,会根据它们在列表中的顺序被检查 |
| 113 | +- 如果对下一个类存在两个合法的选择,选择第一个父类 |
| 114 | + |
| 115 | +## super 原理 |
| 116 | + |
| 117 | +`super` 的工作原理如下: |
| 118 | + |
| 119 | +```python |
| 120 | +def super(cls, inst): |
| 121 | + mro = inst.__class__.mro() |
| 122 | + return mro[mro.index(cls) + 1] |
| 123 | +``` |
| 124 | + |
| 125 | +其中,cls 代表类,inst 代表实例,上面的代码做了两件事: |
| 126 | + |
| 127 | +- 获取 inst 的 MRO 列表 |
| 128 | +- 查找 cls 在当前 MRO 列表中的 index, 并返回它的下一个类,即 mro[index + 1] |
| 129 | + |
| 130 | +当你使用 `super(cls, inst)` 时,Python 会在 inst 的 MRO 列表上搜索 cls 的下一个类。 |
| 131 | + |
| 132 | +现在,让我们回到前面的例子。 |
| 133 | + |
| 134 | +首先看类 C 的 `__init__` 方法: |
| 135 | + |
| 136 | +``` |
| 137 | +super(C, self).__init__() |
| 138 | +``` |
| 139 | + |
| 140 | +这里的 self 是当前 C 的实例,self.__class__.mro() 结果是: |
| 141 | + |
| 142 | +``` |
| 143 | +[__main__.C, __main__.A, __main__.B, __main__.Base, object] |
| 144 | +``` |
| 145 | + |
| 146 | +可以看到,C 的下一个类是 A,于是,跳到了 A 的 `__init__`,这时会打印出 enter A,并执行下面一行代码: |
| 147 | + |
| 148 | +```python |
| 149 | +super(A, self).__init__() |
| 150 | +``` |
| 151 | + |
| 152 | +注意,这里的 self 也是当前 C 的实例,MRO 列表跟上面是一样的,搜索 A 在 MRO 中的下一个类,发现是 B,于是,跳到了 B 的 `__init__`,这时会打印出 enter B,而不是 enter Base。 |
| 153 | + |
| 154 | +整个过程还是比较清晰的,关键是要理解 super 的工作方式,而不是想当然地认为 super 调用了父类的方法。 |
| 155 | + |
| 156 | + |
| 157 | +# 小结 |
| 158 | + |
| 159 | +- 事实上,`super` 和父类没有实质性的关联。 |
| 160 | +- `super(cls, inst)` 获得的是 cls 在 inst 的 MRO 列表中的下一个类。 |
| 161 | + |
| 162 | +# 参考资料 |
| 163 | + |
| 164 | +- [调用父类方法 — python3-cookbook 2.0.0 文档](http://python3-cookbook.readthedocs.io/zh_CN/latest/c08/p07_calling_method_on_parent_class.html) |
| 165 | +- [理解 Python super - laike9m's blog](https://laike9m.com/blog/li-jie-python-super,70/) |
| 166 | +- [python super() - 漩涡鸣人 - 博客园](http://www.cnblogs.com/lovemo1314/archive/2011/05/03/2035005.html) |
| 167 | +- [Python:super函数 | Hom](http://gohom.win/2016/02/23/py-super/) |
| 168 | +- [Python’s super() considered super! | Deep Thoughts by Raymond Hettinger](https://rhettinger.wordpress.com/2011/05/26/super-considered-super/) |
| 169 | +- [Python super() inheritance and needed arguments - Stack Overflow](http://stackoverflow.com/questions/15896265/python-super-inheritance-and-needed-arguments/15896594#15896594) |
| 170 | + |
2 | 171 |
|
0 commit comments