小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Python靜態(tài)方法(staticmethod)、類方法(classmethod)、__str__的用法

 印度阿三17 2019-04-06

一、使用與特性

1.1、使用說明:

一般來說,要使用某個類的方法,需要先實例化一個對象再調(diào)用方法。而使用@staticmethod或@classmethod,就可以不需要實例化,直接通過類名就可以實現(xiàn)調(diào)用。

使用:直接類名.方法名()?來調(diào)用。

1.2、區(qū)別:

@staticmethod不需要表示自身對象的self和自身類的cls參數(shù)(這兩個參數(shù)都不需要添加),就跟使用函數(shù)一樣。

使用:直接類名.屬性名?或?直接類名.方法名。# 直接類名 也可以 直接類名( )

@classmethod也不需要self參數(shù),但第一個參數(shù)需要是表示自身類的cls參數(shù)。

使用:直接類名.屬性名 或?直接類名.方法名。

:兩者定義的裝飾器調(diào)用方法一樣,如果在@staticmethod中要調(diào)用到這個類的一些屬性方法,只能直接類名.屬性名或類名.方法名。而@classmethod因為持有cls參數(shù),可以來調(diào)用類的屬性,類的方法,實例化對象等。

二、靜態(tài)方法(staticmethod) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

通常情況下,在類中定義的所有函數(shù)(注意了,這里說的就是所有,跟self啥的沒關(guān)系,self也只是一個再普通不過的參數(shù)而已)都是對象的綁定方法,對象在調(diào)用綁定方法時會自動將自己作為參數(shù)傳遞給方法的第一個參數(shù)。除此之外還有兩種常見的方法:靜態(tài)方法和類方法,二者是為類量身定制的,但是實例非要使用,也不會報錯。

是一種普通函數(shù),位于類定義的命名空間中,不會對任何實例類型進行操作,python為我們內(nèi)置了函數(shù)staticmethod來把類中的函數(shù)定義成靜態(tài)方法:

class Foo:
    def spam(x,y,z): #類中的一個函數(shù),千萬不要懵逼,self和x啥的沒有不同都是參數(shù)名
        print(x,y,z)
    spam=staticmethod(spam) #把spam函數(shù)做成靜態(tài)方法

基于之前所學裝飾器的知識,@staticmethod 等同于spam=staticmethod(spam),于是:

class Foo:
    @staticmethod #裝飾器
    def spam(x,y,z):
        print(x,y,z)

使用演示:

print(type(Foo.spam)) #類型本質(zhì)就是函數(shù)
Foo.spam(1,2,3) #調(diào)用函數(shù)應該有幾個參數(shù)就傳幾個參數(shù)

f1=Foo()
f1.spam(3,3,3) #實例也可以使用,但通常靜態(tài)方法都是給類用的,實例在使用時喪失了自動傳值的機制

'''
<class 'function'>
2 3
3 3
'''

應用場景:編寫類時需要采用很多不同的方式來創(chuàng)建實例,而我們只有一個__init__函數(shù),此時靜態(tài)方法就派上用場了。

class Date:
    def __init__(self,year,month,day):
        self.year=year
        self.month=month
        self.day=day
    @staticmethod
    def now(): #用Date.now()的形式去產(chǎn)生實例,該實例用的是當前時間
        t=time.localtime() #獲取結(jié)構(gòu)化的時間格式
        return Date(t.tm_year,t.tm_mon,t.tm_mday) #新建實例并且返回
    @staticmethod
    def tomorrow():#用Date.tomorrow()的形式去產(chǎn)生實例,該實例用的是明天的時間
        t=time.localtime(time.time() 86400)
        return Date(t.tm_year,t.tm_mon,t.tm_mday)

a=Date('1987',11,27) #自己定義時間
b=Date.now() #采用當前時間
c=Date.tomorrow() #采用明天的時間

print(a.year,a.month,a.day)
print(b.year,b.month,b.day)
print(c.year,c.month,c.day)

三、類方法(classmethod) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

?類方法是給類用的,類在使用時會將類本身當做參數(shù)傳給類方法的第一個參數(shù),python為我們內(nèi)置了函數(shù)classmethod來把類中的函數(shù)定義成類方法:

class A:
    x=1
    @classmethod
    def test(cls):
        print(cls,cls.x)

class B(A):
    x=2
B.test()

'''
輸出結(jié)果:
<class '__main__.B'> 2
'''

應用場景:

import time
class Date:
    def __init__(self,year,month,day):
        self.year=year
        self.month=month
        self.day=day
    @staticmethod
    def now():
        t=time.localtime()
        return Date(t.tm_year,t.tm_mon,t.tm_mday)

class EuroDate(Date):
    def __str__(self):
        return 'year:%s month:%s day:%s' %(self.year,self.month,self.day)

e=EuroDate.now()
print(e) #我們的意圖是想觸發(fā)EuroDate.__str__,但是結(jié)果為
'''
輸出結(jié)果:
<__main__.Date object at 0x1013f9d68>
'''

因為e就是用Date類產(chǎn)生的,所以根本不會觸發(fā)EuroDate.__str__,解決方法就是用classmethod。

import time
class Date:
    def __init__(self,year,month,day):
        self.year=year
        self.month=month
        self.day=day
    # @staticmethod
    # def now():
    #     t=time.localtime()
    #     return Date(t.tm_year,t.tm_mon,t.tm_mday)

    @classmethod #改成類方法
    def now(cls):
        t=time.localtime()
        return cls(t.tm_year,t.tm_mon,t.tm_mday) #哪個類來調(diào)用,即用哪個類cls來實例化

class EuroDate(Date):
    def __str__(self):
        return 'year:%s month:%s day:%s' %(self.year,self.month,self.day)

e=EuroDate.now()
print(e) #我們的意圖是想觸發(fā)EuroDate.__str__,此時e就是由EuroDate產(chǎn)生的,所以會如我們所愿
'''
輸出結(jié)果:
year:2017 month:3 day:3
'''

強調(diào),注意注意注意:靜態(tài)方法和類方法雖然是給類準備的,但是如果實例去用,也是可以用的,只不過實例去調(diào)用的時候容易讓人混淆,不知道你要干啥。

x=e.now() #通過實例e去調(diào)用類方法也一樣可以使用,靜態(tài)方法也一樣
print(x)
'''
輸出結(jié)果:
year:2017 month:3 day:3
'''

四、附加知識點__str__的用法? ? ??

#__str__定義在類內(nèi)部,必須返回一個字符串類型,
#什么時候會出發(fā)它的執(zhí)行呢?打印由這個類產(chǎn)生的對象時,會觸發(fā)執(zhí)行

class People:
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def __str__(self):
        return '<name:%s,age:%s>' %(self.name,self.age)

p1=People('egon',18)
print(p1)
str(p1) #----->p1.__str__()

?

來源:http://www./content-1-157251.html

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多