python之super的使用小結(jié)
為什么需要super
在python沒有引入super之前, 如果需要在子類中引用父類的方法, 一般寫法如下:
class Father:
def whoami(self):
print("I am father")
class Child:
def whoami(self):
print("I am child")
Father.whoami(self)
這樣看好像沒什么問題, 就算沒有super也能正常調(diào)用父類的方法, 但是如果有一天Father類需要修改類名為Father1, 那么子類Child中也必須跟著修改.
想象下如果一個類有很多個子類, 這樣一來我們就需要修改每個子類中引用父類的語句
怎么使用super
Help on class super in module builtins: class super(object) | super() -> same as super(__class__, <first argument>) | super(type) -> unbound super object | super(type, obj) -> bound super object; requires isinstance(obj, type) | super(type, type2) -> bound super object; requires issubclass(type2, type) | Typical use to call a cooperative superclass method: | class C(B): | def meth(self, arg): | super().meth(arg) | This works for class methods too: | class C(B): | @classmethod | def cmeth(cls, arg): | super().cmeth(arg)
我們來看看super的幫助文檔, 首先super是一個類, 它的調(diào)用方法有如下幾種:
1.super()
2.super(type)
3.super(type, obj)
4.super(type, type2)
我們推薦用第一種方法來使用super, 因?yàn)樗⒉恍枰@式傳遞任何參數(shù).但是要注意一點(diǎn), super只能在新式類中使用.
class A:
def __init__(self):
print("this is A")
class B(A):
def __init__(self):
super().__init__()
print("this is B")
b = B()
"""
this is A
this is B
"""
看起來super就像直接調(diào)用了B的父類A的__init__, 其實(shí)并不是這樣的, 我們看看super在多繼承下的使用
class A:
def __init__(self):
print("this is A")
print("leave A")
class B(A):
def __init__(self):
print("this is B")
super().__init__()
print("leave B")
class C(A):
def __init__(self):
print("this is C")
super().__init__()
print("leave C")
class D(B, C):
def __init__(self):
print("this is D")
super().__init__()
print("leave D")
d = D()
"""
this is D
this is B
this is C
this is A
leave A
leave C
leave B
leave D
"""
print(D.__mro__)
"""
(<class '__main__.D'>,
<class '__main__.B'>,
<class '__main__.C'>,
<class '__main__.A'>,
<class 'object'>)
"""
這里可以看到, 如果super只是簡單調(diào)用父類的方法的話, 那么調(diào)用了B的__init__ 方法之后, 應(yīng)該調(diào)用A的__init__ 才對, 但是調(diào)用的卻是C的__init__ 方法
這是因?yàn)閟uper調(diào)用的是當(dāng)前類在MRO列表中的后一個類, 而并不是簡單地調(diào)用當(dāng)前類的父類
python并沒有強(qiáng)制性限制我們使用super調(diào)用父類, 我們還是可以使用原始的方法來調(diào)用父類中的方法, 但是需要注意的是調(diào)用父類的方法要統(tǒng)一, 即全用super或全不用super, 而用super 的話, 調(diào)用的方式也最好是統(tǒng)一的
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python super用法及原理詳解
- python類中super() 的使用解析
- Python高級編程之繼承問題詳解(super與mro)
- Python 繼承,重寫,super()調(diào)用父類方法操作示例
- python super的使用方法及實(shí)例詳解
- 對Python3之方法的覆蓋與super函數(shù)詳解
- 解決python super()調(diào)用多重繼承函數(shù)的問題
- python3中類的繼承以及self和super的區(qū)別詳解
- python里 super類的工作原理詳解
- Python中super函數(shù)用法實(shí)例分析
- 深入理解Python中的super()方法
- Python中super函數(shù)的用法
- python進(jìn)程管理工具supervisor的安裝與使用教程
- python使用super()出現(xiàn)錯誤解決辦法
- python類中super()和__init__()的區(qū)別
- python super函數(shù)使用方法詳解
相關(guān)文章
python?DataFrame數(shù)據(jù)分組統(tǒng)計(jì)groupby()函數(shù)的使用
在python的DataFrame中對數(shù)據(jù)進(jìn)行分組統(tǒng)計(jì)主要使用groupby()函數(shù),本文主要介紹了python?DataFrame數(shù)據(jù)分組統(tǒng)計(jì)groupby()函數(shù)的使用,具有一定的參考價(jià)值,感興趣的可以了解一下2022-03-03
如何解決cmd運(yùn)行python提示不是內(nèi)部命令
在本篇文章里小編給大家整理了關(guān)于如何解決cmd運(yùn)行python提示不是內(nèi)部命令的相關(guān)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2020-07-07
使用django-suit為django 1.7 admin后臺添加模板
前面我們介紹了Django-grappelli給admin添加模板,可是使用中發(fā)現(xiàn)inline有點(diǎn)問題,所以就換了今天我們要談的Django-suit,貌似要稍微好一些2014-11-11
對python 中re.sub,replace(),strip()的區(qū)別詳解
今天小編就為大家分享一篇對python 中re.sub,replace(),strip()的區(qū)別詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
Python基于gevent實(shí)現(xiàn)文件字符串查找器
這篇文章主要介紹了Python基于gevent實(shí)現(xiàn)文件字符串查找器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
python 實(shí)現(xiàn)手機(jī)自動撥打電話的方法(通話壓力測試)
今天小編就為大家分享一篇python 實(shí)現(xiàn)手機(jī)自動撥打電話的方法(通話壓力測試),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Python 輸出詳細(xì)的異常信息(traceback)方式
這篇文章主要介紹了Python 輸出詳細(xì)的異常信息(traceback)方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04

