python中的classmethod與staticmethod
1.靜態(tài)方法(staticmethod)
靜態(tài)方法:
@staticmethod
也是一個(gè)類方法,是可以直接類調(diào)用的。個(gè)人認(rèn)為的使用場景是:只要要定義的方法里不涉及到self參數(shù),就用靜態(tài)方法承擔(dān)。因?yàn)檫@樣就表明這個(gè)方法和本身的類沒有關(guān)系,明確的區(qū)別出類相關(guān)和不相關(guān)。
class A: ? ? def __init__(self, a, b): ? ? ? ? self.a = a ? ? ? ? self.b = b ? ? def do_normal_something(self, a, b): ? ? ? ? print("do_normal_something",a,b) ? ? @staticmethod ? ? def do_static_something(a,b): ? ? ? ? print('do_static_something',a,b) ? ? @classmethod ? ? def do_class_something(cls): ? ? ? ? pass ? ? def __call__(self,a,b): ? ? ? ? print("123call",a,b) a = A(1,2) a.do_normal_something(7,8) a.do_static_something(5,6)
2.類方法(classmethod)
為什么會(huì)出現(xiàn)classmethod
classmethod
設(shè)計(jì)的目的是什么呢?事實(shí)上與Python面向?qū)ο缶幊逃嘘P(guān)的,由于Python不支持多個(gè)的參數(shù)重載構(gòu)造函數(shù),比方在C++里,構(gòu)造函數(shù)能夠依據(jù)參數(shù)個(gè)數(shù)不一樣。能夠?qū)懚鄠€(gè)構(gòu)造函數(shù)。Python為了解決問題,採用classmethod修飾符的方式,這樣定義出來的函數(shù)就能夠在類對象實(shí)例化之前調(diào)用這些函數(shù),就相當(dāng)于多個(gè)構(gòu)造函數(shù),解決多個(gè)構(gòu)造函數(shù)的代碼寫在類外面的問題。
- 類最基本的作用是實(shí)例化出一個(gè)對象,但是有的時(shí)候在實(shí)例化之前,就需要先和類做一定的交互,這種交互可能會(huì)影響實(shí)際實(shí)例化的過程,所以必須放在調(diào)用構(gòu)造函數(shù)之前。大概也可能是因?yàn)檫@個(gè)原因出現(xiàn)了
classmethod
- 直接一點(diǎn)來說,我們知道對于一個(gè)普通的類,我們要使用其中的方法的話,需要對類進(jìn)行實(shí)例化,而一個(gè)類中,某個(gè)函數(shù)前面加上了
staticmethod
或者classmethod的話,那么這個(gè)函數(shù)就可以不通過實(shí)例化直接調(diào)用,可以通過類名進(jìn)行調(diào)用的 @classmethod
定義的類方法是可選構(gòu)造函數(shù)中,我們定義了一個(gè)類方法,類方法的第一個(gè)參數(shù)(cls)指代的就是類本身。類方法會(huì)用這個(gè)類來創(chuàng)建并返回最終的實(shí)例。使用類方法的另一個(gè)好處就是在繼承的時(shí)候,保證了子類使用可選構(gòu)造函數(shù)構(gòu)造出來的類是子類的實(shí)例而不是父類的實(shí)例。
案例:
class Data_test(object): ? ? def __init__(self, year=0, month=0, day=0): ? ? ? ? self.day = day ? ? ? ? self.month = month ? ? ? ? self.year = year ? ? def out_date(self): ? ? ? ? print('year:',self.year,'month:',self.month,'day:',self.day) t = Data_test(2016, 8, 1) t.out_date()
但是如果用戶輸入的是 “2016-8-1” 這樣的字符格式,那么就需要調(diào)用Date_test
類前做一下處理:
class Data_test(object): ? ? def __init__(self, year=0, month=0, day=0): ? ? ? ? self.day = day ? ? ? ? self.month = month ? ? ? ? self.year = year ? ? def out_date(self): ? ? ? ? print('year:',self.year,'month:',self.month,'day:',self.day) string_date = '2016-8-1' year, month, day = map(int, string_date.split('-')) s = Data_test(year, month, day) s.out_date()
先把‘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
就開始出場了
class Data_test(object): ? ? def __init__(self, year=0, month=0, day=0): ? ? ? ? self.day = day ? ? ? ? self.month = month ? ? ? ? self.year = year ? ? @classmethod ? ? def getdata(cls,str): #cls表示調(diào)用當(dāng)前類名 ? ? ? ? year, month, day = map(int, string_date.split('-')) ? ? ? ? s = cls(year,month,day) ? ? ? ? return s #返回一個(gè)初始化的類 ? ? def out_date(self): ? ? ? ? print('year:',self.year,'month:',self.month,'day:',self.day) string_date = '2016-8-1' s = Data_test.getdata(string_date) s.out_date()
在繼承時(shí)也能工作的很好:
類方法的一個(gè)主要用途就是定義多個(gè)構(gòu)造器。它接受一個(gè)class
作為第一個(gè)參數(shù)(cls)。在繼承時(shí)也能工作的很好:
import time class Data: ? ? #主要構(gòu)造器 ? ? def __init__(self,y,m,d): ? ? ? ? self.year = y ? ? ? ? self.month = m ? ? ? ? self.day = d ? ? ? ? print('year:', self.year, 'month:', self.month, 'day:', self.day) ? ? # 可選擇的構(gòu)造器 ? ? @classmethod ? ? def today(cls): ? ? ? ? t = time.localtime() ? ? ? ? return cls(t.tm_year,t.tm_mon,t.tm_mday) a = Data(2022,1,13) #主要構(gòu)造器 b = Data.today() #可選擇的構(gòu)造器 class NewData(Data): ? ? pass c = NewData.today() #繼承的時(shí)候也可照樣工作
到此這篇關(guān)于python中的classmethod與staticmethod的文章就介紹到這了,更多相關(guān)python的classmethod和staticmethod內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python基于分析Ajax請求實(shí)現(xiàn)抓取今日頭條街拍圖集功能示例
這篇文章主要介紹了Python基于分析Ajax請求實(shí)現(xiàn)抓取今日頭條街拍圖集功能,涉及Python針對今日頭條URL請求與json數(shù)據(jù)處理相關(guān)操作技巧,需要的朋友可以參考下2018-07-07python實(shí)現(xiàn)sublime3的less編譯插件示例
這篇文章主要介紹了python實(shí)現(xiàn)sublime3的less編譯插件示例的相關(guān)資料2014-04-04pywinauto自動(dòng)化測試使用經(jīng)驗(yàn)
本文主要介紹了pywinauto自動(dòng)化測試使用經(jīng)驗(yàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03python定時(shí)任務(wù)schedule庫用法詳細(xì)講解
python中有一個(gè)輕量級的定時(shí)任務(wù)調(diào)度的庫schedule,下面這篇文章主要給大家介紹了關(guān)于python定時(shí)任務(wù)schedule庫用法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01