日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python内置函数(30)——super

發(fā)布時(shí)間:2024/4/17 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python内置函数(30)——super 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

英文文檔:

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.

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.

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).

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.

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).

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].

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.

?  根據(jù)傳入的參數(shù)生成一個(gè)新的子類和父類關(guān)系的代理對(duì)象

說明:

  1. super函數(shù)返回的是一個(gè)代理對(duì)象,通過此對(duì)象可以調(diào)用所在類的父類或者兄弟類的方法,而不顯示的指定父類或者兄弟類的類名。

  2. 為什么要有super?

    最早之前,在子類(B)中調(diào)用父類(A)的方法采用的方式如下:?

#定義父類A >>> class A(object):def __init__(self):print('A.__init__')#實(shí)例化A >>> a = A() A.__init__# 定義子類B,繼承A,在B的__init__ 方法中調(diào)用A的__init__方法 >>> class B(A): def __init__(self):print('B.__init__')A.__init__(self)#實(shí)例化B >>> b = B() B.__init__ A.__init__

    假設(shè)現(xiàn)在要更改新定義一個(gè)類(A1),并更改繼承關(guān)系(B->A改成B->A1),則需要所有類中做如下修改:

#定義新的父類A1 >>> class A1(object):def __init__(self):print('A1.__init__')#更改繼承關(guān)系B->A改成B->A1 >>> class B(A1):def __init__(self):print('B.__init__')A1.__init__(self)#能正確調(diào)用新的父類A1的__init__方法 >>> b = B() B.__init__ A1.__init__#假設(shè)忘了修改A.__init__(self) >>> class B(A1):def __init__(self):print('B.__init__')A.__init__(self)#則還是調(diào)用了A的__init__方法 >>> b = B() B.__init__ A.__init__

    引入super之后,不需要顯示指定父類的類名,增強(qiáng)了程序的可維護(hù)性:

#B->A 改用super方式調(diào)用父類方法 >>> class B(A):def __init__(self):print('B.__init__')super().__init__()#能正確調(diào)用父類方法 >>> b = B() B.__init__ A.__init__#更改繼承關(guān)系B->A改成B->A1,調(diào)用父類方法方式不用修改 >>> class B(A1):def __init__(self):print('B.__init__')super().__init__()#也能正確調(diào)用父類方法 >>> b = B() B.__init__ A1.__init__

  3. 不帶任何參數(shù)的super等效于super(類名,self),此種情況多用于單繼承關(guān)系的子類中。

#super不帶參數(shù) >>> class B(A1):def __init__(self):print('B.__init__')super().__init__()#能正確調(diào)用父類方法 >>> b = B() B.__init__ A1.__init__#super帶兩個(gè)參數(shù)(類名,self) >>> class B(A1):def __init__(self):print('B.__init__')super(B,self).__init__()#也能正確調(diào)用父類方法 >>> b = B() B.__init__ A1.__init__

  4. 如果第2個(gè)參數(shù)不傳入,則表示代理對(duì)象不綁定繼承關(guān)系。

#super第2個(gè)參數(shù)不傳入,生成代理對(duì)象不綁定繼承關(guān)系 >>> class B(A1):def __init__(self):print('B.__init__')super(B).__init__()#super(B).__init__()方法執(zhí)行時(shí)不會(huì)調(diào)用父類方法 >>> b = B() B.__init__

  5. 如果第2個(gè)參數(shù)是一個(gè)對(duì)象,則對(duì)象必須是第1個(gè)參數(shù)指定類型的實(shí)例,此種關(guān)系多用于多層繼承關(guān)系的子類中。

#定義父類A >>> class A(object):def __init__(self):print('A.__init__')#定義子類B,繼承A,__init__中調(diào)用父類的__init__方法 >>> class B(A):def __init__(self):print('B.__init__')super().__init__()#定義子類C,繼承B,__init__中調(diào)用父類的__init__方法 >>> class C(B):def __init__(self):print('C.__init__')super().__init__()#實(shí)例化C時(shí),執(zhí)行C的__init__方法,調(diào)用直接父類B的__init__方法,又進(jìn)一步調(diào)用間接父類A的__init__方法 >>> c = C() C.__init__ B.__init__ A.__init__#重新定義子類C,繼承關(guān)系不變,調(diào)用父類方法__init__時(shí)改用super(B,self) >>> class C(B):def __init__(self):print('C.__init__')super(B,self).__init__()#實(shí)例化C時(shí),執(zhí)行C的__init__方法,super(B,self)代理找到B的父類A,將self轉(zhuǎn)換成B的實(shí)例,直接調(diào)用了A的__init__方法,跳過了調(diào)用B的__init__方法 >>> c = C() C.__init__ A.__init__#定義一個(gè)新類D >>> class D(object):def __init__(self):print('D.__init__')#重新定義C,繼承關(guān)系不變,調(diào)用父類方法__init__時(shí)改用super(D,self) >>> class C(B):def __init__(self):print('C.__init__')super(D,self).__init__()#實(shí)例化C時(shí),執(zhí)行C的__init__方法,super(D,self)代理找到D的父類object,將self轉(zhuǎn)換成D的實(shí)例,因?yàn)镈和C無繼承關(guān)系,self 無法轉(zhuǎn)換成D的實(shí)例所以報(bào)錯(cuò) >>> c= C() C.__init__ Traceback (most recent call last):File "<pyshell#14>", line 1, in <module>c= C()File "<pyshell#13>", line 4, in __init__super(D,self).__init__() TypeError: super(type, obj): obj must be an instance or subtype of type

  6. 如果第2個(gè)參數(shù)時(shí)一個(gè)類型,則類型必須是第1個(gè)參數(shù)指定類型的子類,此種關(guān)系多用于多層繼承關(guān)系的子類中,適用于類方法。

#定義父類A,并定義有一個(gè)類方法sayHello >>> class A(object):@classmethoddef sayHello(cls):print('A.sayHello')# 定義子類B,繼承A,重寫類方法sayHello,在其中調(diào)用父類的sayHello方法 >>> class B(A):@classmethoddef sayHello(cls):print('B.sayHello')super().sayHello()# 定義子類C,繼承B,重寫類方法sayHello,在其中調(diào)用父類的sayHello方法 >>> class C(B):@classmethoddef sayHello(cls):print('C.sayHello')super().sayHello()#調(diào)用C的類方法sayHello,其調(diào)用C的直接父類B的類方法sayHello,調(diào)用時(shí)B的sayHello方法又調(diào)用B的直接父類A的類方法sayHello >>> C.sayHello() C.sayHello B.sayHello A.sayHello#重新定義類C,繼承關(guān)系不變,使用super(C,C)的方式調(diào)用父類方法 >>> class C(B):@classmethoddef sayHello(cls):print('C.sayHello')super(C,C).sayHello()#調(diào)用C的類方法sayHello,super(C,C)代理對(duì)象,找到C的直接父類B,然后調(diào)用C的直接父類B的類方法sayHello,調(diào)用時(shí)B的sayHello方法又調(diào)用B的直接父類A的類方法sayHello >>> C.sayHello() C.sayHello B.sayHello A.sayHello#重新定義類C,繼承關(guān)系不變,使用super(B,C)的方式調(diào)用父類方法 >>> class C(B):@classmethoddef sayHello(cls):print('C.sayHello')super(B,C).sayHello()#調(diào)用C的類方法sayHello,super(B,C)代理對(duì)象,找到B的直接父類A,然后調(diào)用B的直接父類A的類方法sayHello,中間不會(huì)調(diào)用B的sayHello方法 >>> C.sayHello() C.sayHello A.sayHello#定義一個(gè)新類D,和A、B、C無繼承關(guān)系 >>> class D(object):@classmethoddef sayHello(cls):print('D.sayHello')#重新定義類C,繼承關(guān)系不變,使用super(D,C)的方式調(diào)用父類方法 >>> class C(B):@classmethoddef sayHello(cls):print('C.sayHello')super(D,C).sayHello()#調(diào)用C的類方法sayHello,super(D,C)代理對(duì)象,找到B的直接父類object,然后將C轉(zhuǎn)換成D類,轉(zhuǎn)換失敗調(diào)用出錯(cuò) >>> C.sayHello() C.sayHello Traceback (most recent call last):File "<pyshell#81>", line 1, in <module>C.sayHello()File "<pyshell#80>", line 5, in sayHellosuper(D,C).sayHello() TypeError: super(type, obj): obj must be an instance or subtype of type

?

轉(zhuǎn)載于:https://www.cnblogs.com/lincappu/p/8144873.html

總結(jié)

以上是生活随笔為你收集整理的Python内置函数(30)——super的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 麻豆成人av | 国产在线拍揄自揄拍无码 | 毛片视频免费观看 | 精品国产精品国产偷麻豆 | 好吊视频一区二区三区 | 114国产精品久久免费观看 | 中国挤奶哺乳午夜片 | 国产aa视频 | 干日本少妇首页 | 亚洲国产电影在线观看 | 亚洲自拍小视频 | 色婷婷亚洲一区二区三区 | 人人澡人人澡人人澡 | 欧美精品videos极品 | 福利免费在线观看 | 成人高清网站 | 韩国av免费在线 | 亚洲精品在线观看免费 | 狠狠网| 你懂的在线播放 | 黄色影音| 日批视屏 | 毛片网站在线播放 | 亚洲精品中文无码AV在线播放 | 女人被男人躁得好爽免费视频 | 女性私密整形视频 | 国产精品黄色大片 | 91丨九色丨国产在线 | 性一交一乱一伧国产女士spa | 伊人网免费视频 | 国产精品无码中文 | 亚洲视频导航 | 99久久伊人 | 色图视频 | 精品一区二区三区日韩 | 性户外野战hd | 日日操夜夜干 | 99精品欧美一区二区三区 | 逼特逼在线视频 | 亚洲日本精品一区 | 玩弄丰满少妇xxxxx性多毛 | 日韩专区一区二区三区 | 天堂网在线资源 | 理伦毛片 | 黄色靠逼视频 | 日韩久久久久久久久 | 黄色aaa毛片 | 黄色工厂在线观看 | 国产又粗又长又大视频 | 先锋影音一区二区 | 羞羞涩| 日韩在线一二三 | 国产精品白浆一区二小说 | 91精品人妻一区二区三区 | 99黄色| 少妇高潮一区二区三区四区 | 免费在线观看视频 | 午夜神马福利 | 手机免费在线观看av | 免费性网站 | 葵司免费一区二区三区四区五区 | 欧美日韩成人一区二区三区 | 特级黄色录像 | 欧美精品色哟哟 | 亚洲精品视频一区二区 | 久操视频在线播放 | www.日本在线 | 一区二区三区在线免费视频 | 婷婷四月 | 久热精品在线视频 | 亚洲aa视频 | 波多野结衣福利视频 | 色天天av | 日本人妻伦在线中文字幕 | 免费看的av网站 | 亚洲天堂网络 | 亚洲欧洲中文字幕 | 福利视频黄色 | 久久22| 国产精品3 | √天堂中文官网8在线 | 中文字幕乱码一区二区 | 久久久久亚洲AV成人网人人小说 | 精品国产91久久久久久久妲己 | 噼里啪啦国语高清 | 东方成人av | 织田真子作品 | 污污在线免费观看 | 国产无套粉嫩白浆内谢 | 国产美女裸体无遮挡免费视频 | 东北少妇高潮抽搐 | 少妇无套内谢久久久久 | 四虎在线观看视频 | 久久精品在线 | 特一级黄色大片 | 好男人天堂网 | h片网站在线观看 | 性感美女一区 | 国内自拍99 |