詳解Python中@staticmethod和@classmethod區(qū)別及使用示例代碼
本文主要介紹Python中,class(類(lèi))的裝飾器@staticmethod和@classmethod的使用示例代碼和它們的區(qū)別。
1、@staticmethod和@classmethod區(qū)別
@staticmethod:靜態(tài)方法
@classmethod:類(lèi)方法
一般來(lái)說(shuō),要使用某個(gè)類(lèi)的方法,需要先實(shí)例化一個(gè)對(duì)象再調(diào)用方法。
而使用@staticmethod或@classmethod,就可以不需要實(shí)例化,直接通過(guò)類(lèi)名就可以實(shí)現(xiàn)調(diào)用
使用:直接類(lèi)名.方法名()來(lái)調(diào)用。@staticmethod和@classmethod都可以直接類(lèi)名.方法名()來(lái)調(diào)用,
@staticmethod不需要表示自身對(duì)象的self和自身類(lèi)的cls參數(shù)(這兩個(gè)參數(shù)都不需要添加),就跟使用函數(shù)一樣。
使用:直接類(lèi)名.屬性名或直接類(lèi)名.方法名。
@classmethod也不需要self參數(shù),但第一個(gè)參數(shù)需要是表示自身類(lèi)的cls參數(shù)。
使用:直接類(lèi)名.屬性名或直接類(lèi)名.方法名。
兩者定義的裝飾器調(diào)用方法一樣,但是@classmethod裝飾器定義的類(lèi)方法需要傳入類(lèi)參數(shù)cls。
@staticmethod中要調(diào)用到這個(gè)類(lèi)的一些屬性方法,只能直接類(lèi)名.屬性名或類(lèi)名.方法名。
而@classmethod有cls參數(shù),可以來(lái)調(diào)用類(lèi)的屬性,類(lèi)的方法,實(shí)例化對(duì)象等,避免硬編碼更靈活。
2、@staticmethod和@classmethod使用示例代碼
class A(object): def foo(self, x): print "executing foo(%s, %s)" % (self, x) @classmethod def class_foo(cls, x): print "executing class_foo(%s, %s)" % (cls, x) @staticmethod def static_foo(x): print "executing static_foo(%s)" % x a = A() #通過(guò)實(shí)例調(diào)用方法,對(duì)象實(shí)例a作為第一個(gè)參數(shù)隱式傳遞。 a.foo (1) # executing foo(<__main__.A object at 0xb7dbef0c>,1) #對(duì)于類(lèi)方法,對(duì)象實(shí)例的類(lèi)將隱式地作為第一個(gè)參數(shù)而不是傳遞self a.class_foo(1) # executing class_foo(<class '__main__.A'>,1) #使用這個(gè)類(lèi)調(diào)用class_foo A.class_foo(1) # executing class_foo(<class '__main__.A'>,1) #對(duì)于staticmethods,self(對(duì)象實(shí)例)和cls(類(lèi))都不會(huì)作為第一個(gè)參數(shù)隱式傳遞。它們的行為類(lèi)似普通函數(shù),除了你可以從實(shí)例或類(lèi)中調(diào)用它們 a.static_foo(1) # executing static_foo(1) A.static_foo('hi') # executing static_foo(hi) print(a.foo) # <bound method A.foo of <__main__.A object at 0xb7d52f0c>> print(a.class_foo) # <bound method type.class_foo of <class '__main__.A'>> print(a.static_foo) # <function static_foo at 0xb7d479cc> print(a.static_foo) # <function static_foo at 0xb7d479cc>
總結(jié)一下彼此的調(diào)用區(qū)別:
到此這篇關(guān)于詳解Python中@staticmethod和@classmethod區(qū)別及使用示例代碼的文章就介紹到這了,更多相關(guān)Python @staticmethod和@classmethod內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python標(biāo)準(zhǔn)庫(kù)中內(nèi)置裝飾器@staticmethod和@classmethod
- Python中類(lèi)方法@classmethod和靜態(tài)方法@staticmethod解析
- Python?class類(lèi)@staticmethod及@classmethod區(qū)別淺析
- 詳解python中@classmethod和@staticmethod方法
- Python 類(lèi)方法和實(shí)例方法(@classmethod),靜態(tài)方法(@staticmethod)原理與用法分析
- Python中@classmethod和@staticmethod的區(qū)別
相關(guān)文章
詳解Python中的array數(shù)組模塊相關(guān)使用
數(shù)組并不是Python中內(nèi)置的標(biāo)配數(shù)據(jù)結(jié)構(gòu),不過(guò)擁有array模塊我們也可以在Python中使用數(shù)組結(jié)構(gòu),下面我們就來(lái)詳解詳解Python中的array數(shù)組模塊相關(guān)使用2016-07-07python DataFrame轉(zhuǎn)dict字典過(guò)程詳解
這篇文章主要介紹了python DataFrame轉(zhuǎn)dict字典過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Python和Matlab實(shí)現(xiàn)蝙蝠算法的示例代碼
蝙蝠算法是一種搜索全局最優(yōu)解的有效方法,本文主要介紹了Python和Matlab實(shí)現(xiàn)蝙蝠算法的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03python中字典(Dictionary)用法實(shí)例詳解
這篇文章主要介紹了python中字典(Dictionary)用法,以實(shí)例形式較為詳細(xì)的分析了Python字典建立、添加、刪除等常見(jiàn)操作技巧,需要的朋友可以參考下2015-05-05基于python tornado實(shí)現(xiàn)圖床功能
因?yàn)橘I(mǎi)了阿里/騰訊的云服務(wù)器,但是使用云存儲(chǔ)還需要收費(fèi),又加上家里正好有一臺(tái)nas,又加上閑的沒(méi)事,所以搞了一個(gè)小腳本,這個(gè)項(xiàng)目主要功能是為typora增加一個(gè)自定義圖床,本文給大家介紹基于python tornado實(shí)現(xiàn)圖床功能,感興趣的朋友一起看看吧2023-08-08Python去除html標(biāo)簽的幾種方法總結(jié)
這篇文章主要介紹了Python去除html標(biāo)簽的幾種方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01python3下使用cv2.imwrite存儲(chǔ)帶有中文路徑圖片的方法
今天小編就為大家分享一篇python3下使用cv2.imwrite存儲(chǔ)帶有中文路徑圖片的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05