python @classmethod 的使用場(chǎng)合詳解
官方的說(shuō)法:
classmethod(function)
中文說(shuō)明:
classmethod是用來(lái)指定一個(gè)類的方法為類方法,沒(méi)有此參數(shù)指定的類的方法為實(shí)例方法,使用方法如下:
class C: @classmethod def f(cls, arg1, arg2, ...): ...
看后之后真是一頭霧水。說(shuō)的啥子?xùn)|西呢???
自己到國(guó)外的論壇看其他的例子和解釋,頓時(shí)就很明朗。 下面自己用例子來(lái)說(shuō)明。
看下面的定義的一個(gè)時(shí)間類:
class Data_test(object): day=0 month=0 year=0 def __init__(self,year=0,month=0,day=0): self.day=day self.month=month self.year=year def out_date(self): print "year :" print self.year print "month :" print self.month print "day :" print self.day t=Data_test(2016,8,1) t.out_date()
輸出:
year : 2016 month : 8 day : 1
符合期望。
如果用戶輸入的是 "2016-8-1" 這樣的字符格式,那么就需要調(diào)用Date_test 類前做一下處理:
string_date='2016-8-1' year,month,day=map(int,string_date.split('-')) s=Data_test(year,month,day)
先把‘2016-8-1' 分解成 year,month,day 三個(gè)變量,然后轉(zhuǎn)成int,再調(diào)用Date_test(year,month,day)函數(shù)。 也很符合期望。
那我可不可以把這個(gè)字符串處理的函數(shù)放到 Date_test 類當(dāng)中呢?
那么@classmethod 就開(kāi)始出場(chǎng)了
class Data_test2(object): day=0 month=0 year=0 def __init__(self,year=0,month=0,day=0): self.day=day self.month=month self.year=year @classmethod def get_date(cls,data_as_string): #這里第一個(gè)參數(shù)是cls, 表示調(diào)用當(dāng)前的類名 year,month,day=map(int,string_date.split('-')) date1=cls(year,month,day) #返回的是一個(gè)初始化后的類 return date1 def out_date(self): print "year :" print self.year print "month :" print self.month print "day :" print self.day
在Date_test類里面創(chuàng)建一個(gè)成員函數(shù), 前面用了@classmethod裝飾。 它的作用就是有點(diǎn)像靜態(tài)類,比靜態(tài)類不一樣的就是它可以傳進(jìn)來(lái)一個(gè)當(dāng)前類作為第一個(gè)參數(shù)。
那么如何調(diào)用呢?
r=Data_test2.get_date("2016-8-6") r.out_date()
輸出:
year : 2016 month : 8 day : 1
這樣子等于先調(diào)用get_date()對(duì)字符串進(jìn)行出來(lái),然后才使用Data_test的構(gòu)造函數(shù)初始化。
這樣的好處就是你以后重構(gòu)類的時(shí)候不必要修改構(gòu)造函數(shù),只需要額外添加你要處理的函數(shù),然后使用裝飾符 @classmethod 就可以了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python標(biāo)準(zhǔn)庫(kù)中內(nèi)置裝飾器@staticmethod和@classmethod
- Python中通過(guò)@classmethod 實(shí)現(xiàn)多態(tài)的示例
- python中的class_static的@classmethod的巧妙用法
- 詳解Python中@staticmethod和@classmethod區(qū)別及使用示例代碼
- Python 類方法和實(shí)例方法(@classmethod),靜態(tài)方法(@staticmethod)原理與用法分析
- 對(duì)Python中的@classmethod用法詳解
- Python類方法@classmethod()的具體使用
相關(guān)文章
window11系統(tǒng)下Python3.11安裝numpy庫(kù)超詳細(xì)教程
這篇文章主要給大家介紹了關(guān)于window11系統(tǒng)下Python3.11安裝numpy庫(kù)的相關(guān)資料,NumPy是Python的第三方擴(kuò)展包,但它并沒(méi)有包含在Python標(biāo)準(zhǔn)庫(kù)中,因此您需要單獨(dú)安裝它,需要的朋友可以參考下2023-12-12Anaconda中導(dǎo)出環(huán)境的實(shí)現(xiàn)步驟
在 Anaconda 中導(dǎo)出環(huán)境是一種常用的做法,可以將當(dāng)前的環(huán)境配置導(dǎo)出到一個(gè)文件中,本文主要介紹了Anaconda中導(dǎo)出環(huán)境的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值2024-05-05解決Keras中循環(huán)使用K.ctc_decode內(nèi)存不釋放的問(wèn)題
這篇文章主要介紹了解決Keras中循環(huán)使用K.ctc_decode內(nèi)存不釋放的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06Python Paramiko模塊中exec_command()和invoke_shell()兩種操作區(qū)別
invoke_shell 使用 SSH shell channel,而 exec_command 使用 SSH exec channel,本文主要介紹了Python Paramiko模塊中exec_command()和invoke_shell()兩種操作區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02Python編程pytorch深度卷積神經(jīng)網(wǎng)絡(luò)AlexNet詳解
AlexNet和LeNet的架構(gòu)非常相似。這里我們提供了一個(gè)稍微精簡(jiǎn)版本的AlexNet,去除了當(dāng)年需要兩個(gè)小型GPU同時(shí)運(yùn)算的設(shè)計(jì)特點(diǎn)2021-10-10