python的继承用法_Python 中的继承之Super用法
以下Copy自官方文檔說明,可點擊查看官網源文
翻譯內容屬于德德自譯,有不當之處請指正,勿噴。。。
翻譯括弧中是德德自己理解,通過代碼驗證的,勿噴。。。
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.
返回一個代理對象,該對象可以把方法調用委托給父類或者兄弟類(返回類型為super類型,值為一個對象)。這對于一個類訪問繼承的被重寫函數很有用的(比較繞,意思就是這個可以訪問父類或者兄弟類的被重寫函數)。查找順序和getattr()一樣,但本身類型要跳過(getattr()的順序是什么。。。自己查去吧,但是下面有講,他的順序為MRO方式的順序)。
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.
一個類型(我覺得他想表達的是類class,新型類而不是所有的type)的_mro_屬性列舉了 getattr()&super()兩個函數的方法調用順序(調用算法是C3,有興趣自己上網搜啊,德德覺得了解原理沒啥用處,哈哈)。這個屬性是動態變化的,它根隨繼承層次結構的更新而變化。
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對象就是未綁定的(德德沒明白啥意思哈,感覺本菊花沒用。。或者作者的意圖是告訴你他不能省略,省略了就綁定不了對象)。如果第二參數為一個對象,那么isinstance(obj,type)一定為true,如果第二個參數是一個類,那么issubclass(obj,type)一定得為true。(本段話超級有內涵啊。。。一般人還真不容易讀懂,當然德德不是一般人啦 哈哈。首先本段講的是super的使用方法,根據定義可以看到第一個參數是一個類型,第二個參數可以是缺省,對象,或者類型。缺省嘛。。。德德表示沒有用或者德德不會用,不解釋。對象嗎,貌似最常見的就是對象了,不多解釋,解釋就是掩飾。然后重點來了,類型,類型的話咋個用來。。。 德德表示不告訴你。O(∩_∩)O,首先要告訴你的是一個小竅門,super的返回值和其第二個參數是一樣的,如果第二個參數是對象,則返回的也是一個對象,如果第二個參數是一個類則返回的也是一個類型。也就是說有 class A,class B(A),b = B() 你可以super(A,b).__init__() 來用 也可以super(A,B).__init__(b)來用。。。自己敲一下代碼就知道了)
感覺德德是個話撈子。。。。好多啊,但是木有辦法精簡啊,而且貌似排版有點小亂。。。表示markdown用的不熟。。O(∩_∩)O
Note super() only works for new-style classes.
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()只適用于新型類(新型類就是object的子類,也就是說class A(object),繼承了object的A就是新型類了)
super用兩個經典的用法。在單繼承的層次結構里,super用來直接引用父類,而不是用類名來引用。這樣可以使代碼更加可維護。這個用法和其他編程語言差不多。(該用法類似于java中的super,java只支持單繼承)
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).
第二種用法,就是支持動態執行環境下相互聯系多繼承方式。(這句話不好翻譯,因為需要理解著翻譯。最簡單的例子就是class A(object), class B(A), class C(A) ,class D(B,C),然后A,B,C,D就形成了cooperative multiple inheritance).這個用法是python特有的,并沒有出現在靜態編譯語言或者單繼承方式的語言。這使它可以實現”鉆石結構圖” ,也就是多個基類實現同一個方法(德德忍不住吐槽一下,太tm繞了,德德也是在多次分析研究下弄明白了該句話的意思。最可氣的是文檔作者還用了一個比喻的修辭手法,尼瑪呀。。。我們這種英語菜的小蟲蟲怎么知道什么叫做‘鉆石圖’啊,次奧。。。不多解釋,我上面關于cooperative multiple inheritance的例子就是‘鉆石圖’^_^)。好的設計要求一個方法在每種情況下都一同一種調用形式(這里是小括號翻譯,因為調用順序實在運行時決定的,因為順序可以適配類結構圖的變化,因為順序中可能包括兄弟類運行時未知優先級順序)。(忍不住啦,次奧。。。 這句話告訴我們Python的這種功能,利于更好的設計,因為在運行的時候,調用順序,類結構,兄弟類的優先級都是不可控的。。。)
For both use cases, a typical superclass call looks like this:
對于兩種用法,一個經典的調用方式示例如下:
class C(B):
def method(self, arg):
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__() 來通過MRO的順序查找類。因此super()未定義語句的隱式查找或super()[name]這種操作符。(德德的英語有點跟不上啊,德德只測試了__getitem__()函數,表示完全能用,也就是說super對于內部函數還是支持的,值得注意的是object類對內部函數沒怎么實現,也就是說再object的直接子類里不要用super。但是super()[name]不能使用,有點混亂,一般__getitem__()是和[]操作符是綁定的,但是不知道咋回事他就是不支持。。。可能是德德理解的不夠深刻)
Also note that super() is not limited to use inside methods. The two argument form specifies the arguments exactly and makes the appropriate references.
同時需要注意的是super()在內部函數是不受限制的,兩個參數的形式表示能夠精確的指定參數和作出準確的引用
For practical suggestions on how to design cooperative classes using super(), see guide to using super().
想要更使用的解說,參考向導using super().
New in version 2.2.
super 是python 2.2 以上新出的
總結
以上是生活随笔為你收集整理的python的继承用法_Python 中的继承之Super用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql设置参数不生效_关于mysql
- 下一篇: java初始化实例化_java类的初始化