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

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

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

python

python替代技术,Python超级方法和调用替代品

發(fā)布時(shí)間:2024/8/5 python 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python替代技术,Python超级方法和调用替代品 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

I see everywhere examples that super-class methods should be called by:

super(SuperClass, instance).method(args)

Is there any disadvantage to doing:

SuperClass.method(instance, args)

解決方案

Consider the following situation:

class A(object):

def __init__(self):

print('Running A.__init__')

super(A,self).__init__()

class B(A):

def __init__(self):

print('Running B.__init__')

# super(B,self).__init__()

A.__init__(self)

class C(A):

def __init__(self):

print('Running C.__init__')

super(C,self).__init__()

class D(B,C):

def __init__(self):

print('Running D.__init__')

super(D,self).__init__()

foo=D()

So the classes form a so-called inheritance diamond:

A

/ \

B C

\ /

D

Running the code yields

Running D.__init__

Running B.__init__

Running A.__init__

That's bad because C's __init__ is skipped. The reason for that is because B's __init__ calls A's __init__ directly.

The purpose of super is to resolve inheritance diamonds. If you un-comment

# super(B,self).__init__()

and comment-out

A.__init__(self)

the code yields the more desireable result:

Running D.__init__

Running B.__init__

Running C.__init__

Running A.__init__

Now all the __init__ methods get called. Notice that at the time you define B.__init__ you might think that super(B,self).__init__() is the same as calling A.__init__(self), but you'd be wrong. In the above situation, super(B,self).__init__() actually calls C.__init__(self).

Holy smokes, B knows nothing about C, and yet super(B,self) knows to call C's __init__? The reason is because self.__class__.mro() contains C. In other words, self (or in the above, foo) knows about C.

So be careful -- the two are not fungible. They can yield vastly different results.

Using super has pitfalls. It takes a considerable level of coordination between all the classes in the inheritance diagram. (They must, for example, either have the same call signature for __init__, since any particular __init__ would not know which other __init__ super might call next, or

else use **kwargs.) Furthermore, you must be consistent about using super everywhere. Skip it once (as in the above example) and you defeat the entire purpose of super.

See the link for more pitfalls.

If you have full control over your class hierarchy, or you avoid inheritance diamonds, then there is no need for super.

總結(jié)

以上是生活随笔為你收集整理的python替代技术,Python超级方法和调用替代品的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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

主站蜘蛛池模板: 黑人操亚洲女人 | 免费裸体视频女性 | 五月婷丁香 | 天天躁日日躁狠狠躁免费麻豆 | 亚洲视频免费观看 | 成年人免费毛片 | 日韩无遮挡 | 伊人中文在线 | 一级丰满大乳hd高清 | 福利第一页 | 夜夜嗨一区二区三区 | 日本一本在线 | 成人在线免费播放视频 | 伊人春色影院 | 九色一区 | 国内特级毛片 | 久久黄色片 | 大陆av片| 亚洲自拍三区 | 国产精品视频无码 | 五月激情小说 | 特一级黄色大片 | 国产成人精品一区二三区 | 午夜视频免费看 | 又黄又色又爽的视频 | 日韩黄色片 | 欧美不卡在线视频 | 午夜激情视频在线播放 | 亚洲一级黄色片 | 天天操天天舔天天干 | 成人av一区二区三区在线观看 | 亚洲AV无码一区二区三区少妇 | 久久三级视频 | 闫嫩的18sex少妇hd | 国产福利片在线 | 国产香蕉视频在线播放 | 欧美黄色三级视频 | 日本在线一 | 五月天久久久久 | 91拔萝卜视频 | 国产亚洲小视频 | 国产任你操 | 蜜桃啪啪 | 女生扒开尿口 | 日本人性爱视频 | 欧美另类z0zx974 | 国产黄色网址在线观看 | √资源天堂中文在线视频 | 长篇乱肉合集乱500小说日本 | 蜜桃免费av| 尤物视频免费观看 | 亚洲国产精品国自产拍久久 | 国产精品一级无码 | 熟女俱乐部一区二区 | 狠色综合7777夜色撩人 | 五月的婷婷 | 精品国产伦一区 | 中日韩精品一区二区三区 | 男女乱淫| 国产色黄| 国产欧美一区二区三区视频 | 成年人看的黄色 | 污网站在线看 | 超碰综合网 | 国产人成无码视频在线观看 | 日韩av成人在线观看 | 黄色长视频 | 国产xxx视频 | 日韩精品一区二区三 | 精品一区二区三区视频在线观看 | 久久成人毛片 | 12av在线| 老熟妇仑乱一区二区视频 | 牛牛影视一区二区 | 日本在线观看一区二区三区 | 亚洲国产日韩在线观看 | 亚洲精品一区久久久久久 | 欧美日韩视频 | 日韩欧美123 | 亚洲免费久久 | 爱情岛论坛亚洲线路一 | 999精彩视频 | 黄色动漫免费在线观看 | 在线麻豆av | 成年免费在线观看 | 国产99re| 精品人伦一区二区三区蜜桃网站 | 四虎4hu | 成人中文字幕在线 | 久久久九九九九 | 撕开少妇裙子猛然进入 | 黄色三级图片 | 色午夜婷婷| 亚洲精品乱码久久久久久蜜桃图片 | 午夜a视频 | 99久久久无码国产 | 牛牛澡牛牛爽一区二区 | 日本丰满熟妇videossex一 | 逼特逼在线视频 |