1、来自python官网的解释:
super([type[, object-or-type]])
Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class. The search order is same as that used by getattr() except that the type itself is skipped.
返回一个代理对象,它将方法调用委托给type的父类或兄弟类。 这对于访问在类中被覆盖的继承方法很有用。 搜索顺序与getattr()使用的顺序相同,除了type本身被跳过。
The __mro__ attribute of the type lists the method resolution search order used by both getattr() and super(). The attribute is dynamic and can change whenever the inheritance hierarchy is updated.
type的__mro__属性列出了getattr()和super()使用的方法解析搜索顺序。该属性是动态的,并且可以在继承层次结构更新时更改。
If the second argument is omitted, the super object returned is unbound. If the second argument is an object, isinstance(obj, type) must be true. If the second argument is a type, issubclass(type2, type) must be true (this is useful for classmethods).
如果省略第二个参数,则返回的super对象是未绑定的。如果第二个参数是一个object,isinstance(obj,type)必须为true。如果第二个参数是type,issubclass(type2,type)必须为true(这对类方法很有用)。
There are two typical use cases for super. In a class hierarchy with single inheritance, super can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable. This use closely parallels the use of super in other programming languages.
super有两个典型的用例。在具有单继承的类层次结构中,super可以用于引用父类而不明确命名它们,从而使代码更易于维护。这种使用与其他编程语言中super的使用非常相似。
The second use case is to support cooperative multiple inheritance in a dynamic execution environment. This use case is unique to Python and is not found in statically compiled languages or languages that only support single inheritance. This makes it possible to implement “diamond diagrams” where multiple base classes implement the same method. Good design dictates that this method have the same calling signature in every case (because the order of calls is determined at runtime, because that order adapts to changes in the class hierarchy, and because that order can include sibling classes that are unknown prior to runtime).
第二种用例是在动态执行环境中支持协同多重继承。此用例是Python独有的,在静态编译语言或仅支持单继承的语言中找不到。这使得它可以实现“菱形图”,其中多个基类实现相同的方法。良好的设计指明此方法在每种情况下具有相同的调用签名(因为调用的顺序在运行时确定,因为该顺序适应类层次结构中的更改,并且因为该顺序可以包括在运行时之前未知的兄弟类)。
For both use cases, a typical superclass call looks like this:
对于这两种用例,典型的超类调用看起来像这样:
class C(B): def method(self, arg): super().method(arg) # This does the same thing as: # super(C, self).method(arg)
Note that super() is implemented as part of the binding process for explicit dotted attribute lookups such as super().__getitem__(name). It does so by implementing its own __getattribute__() method for searching classes in a predictable order that supports cooperative multiple inheritance. Accordingly, super() is undefined for implicit lookups using statements or operators such as super()[name].
注意,super()被实现为明确的点属性查找的绑定过程的一部分,例如super().__ getitem __(name)。 它通过实现自己的__getattribute __()方法来搜索类,以支持协同多重继承的可预测顺序。 因此,对于使用诸如super()[name]之类的语句或操作符的隐式查找,未定义super()。
Also note that, aside from the zero argument form, super() is not limited to use inside methods. The two argument form specifies the arguments exactly and makes the appropriate references. The zero argument form only works inside a class definition, as the compiler fills in the necessary details to correctly retrieve the class being defined, as well as accessing the current instance for ordinary methods.
还要注意,除了零参数形式,super()不限于使用内部方法。两个参数形式完全指定参数,并进行适当的引用。 零参数形式仅在类定义中工作,因为编译器填充必要的细节以正确检索正在定义的类,以及访问普通方法的当前实例。
For practical suggestions on how to design cooperative classes using super(), see guide to using super().
有关如何使用super()设计协同类的实际建议,请参阅guide to using super()。