深入淺析python 中的self和cls的區(qū)別
python 中的self和cls
一句話描述:self是類(Class)實(shí)例化對(duì)象,cls就是類(或子類)本身,取決于調(diào)用的是那個(gè)類。
@staticmethod 屬于靜態(tài)方法裝飾器,@classmethod屬于類方法裝飾器。我們需要從聲明和使用兩個(gè)方面來理解。
詳細(xì)介紹
一般來說,要使用某個(gè)類的方法,需要先⚠️實(shí)例化一個(gè)對(duì)象再調(diào)用方法。而使用@staticmethod或@classmethod,就可以不需要實(shí)例化,直接類名.方法名()來調(diào)用。這有利于組織代碼,把某些應(yīng)該屬于某個(gè)類的函數(shù)給放到那個(gè)類里去,同時(shí)有利于命名空間的整潔。🤔
首先定義一個(gè)類A,類A中有三個(gè)函數(shù),foo1為靜態(tài)函數(shù),用@staticmethod裝飾器裝飾,這種方法與類有某種關(guān)系但不需要使用到實(shí)例或者類來參與。
class A(object):
a = 'a'
@staticmethod
def foo1(name):
print('hello', name, A.a)
def foo2(self, name):
print('hello', name, self.a)
@classmethod
def foo3(cls, name):
print('hello', name, cls.a)
class B(A):
a = 'b'
@staticmethod
def foo1(name):
print('hello', name, B.a)
def foo2(self, name):
print('subclass B')
print('hello', name, self.a)
@classmethod
def foo3(cls, name):
print('hello', name, cls.a)
如下兩種方法都可以正常輸出,也就是說
既可以作為類的方法使用,也可以作為類的實(shí)例的方法使用。
a = A()
b = B()
a.foo1("小熊貓") # hello 小熊貓
A.foo1("小熊貓") # hello 小熊貓
b.foo1("大熊貓") # subclass B, hello 大熊貓 b
B.foo1("大熊貓") # subclass B, hello 大熊貓 b
foo2為正常的函數(shù),是類的實(shí)例的函數(shù),調(diào)用方式如下。
將實(shí)參實(shí)例化對(duì)象或者類名稱傳入self對(duì)象,取到不同的屬性和方法。
a.foo2("小熊貓") # hello 小熊貓 a
A.foo2(a, "小熊貓") # hello 小熊貓 a
A.foo2(b, "小熊貓") # hello 小熊貓 b
A.foo2(A, "小熊貓") # hello 小熊貓 a
A.foo2(B, "小熊貓") # hello 小熊貓 b
B.foo2(a, "小熊貓") # subclass B, hello 小熊貓 a
foo3為類函數(shù),cls作為第一個(gè)參數(shù)用來表示類本身. 在類方法中用到,類方法是只與類本身有關(guān)而與實(shí)例無關(guān)的方法。如下兩種方法都可以正常輸出。
可以看出,傳入形參cls的值為前面的調(diào)用函數(shù),如果再傳入對(duì)象或者類名稱,會(huì)報(bào)類型錯(cuò)誤,多傳了一個(gè)參數(shù)。
a.foo3("小熊貓")
A.foo3("小熊貓")
# a.foo3(a, "小熊貓") # TypeError: foo3() takes 2 positional arguments but 3 were given
# A.foo3(A, "小熊貓") # TypeError: foo3() takes 2 positional arguments but 3 were given
b.foo3("大熊貓")
B.foo3("大熊貓")
@staticmethod和@classmethod的用法
相同:
@staticmethod和@classmethod都可以直接類名.方法名()來調(diào)用
區(qū)別:
- 從它們的使用上來看,@staticmethod不需要表示自身對(duì)象的self和自身類的cls參數(shù),就跟使用函數(shù)一樣。@classmethod也不需要self參數(shù),但第一個(gè)參數(shù)需要是表示自身類的cls參數(shù)。
- 如果在@staticmethod中要調(diào)用到這個(gè)類的一些屬性方法,只能直接類名.屬性名或類名.方法名。
- 而@classmethod因?yàn)槌钟衏ls參數(shù),可以來調(diào)用類的屬性,類的方法,實(shí)例化對(duì)象等,避免硬編碼。
class A(object):
a = 'a'
@staticmethod
def foo1(name):
print('hello foo1', name, A.a)
print("hello foo4 ", B.foo2(B, "小熊貓"))
def foo2(self, name):
print('hello foo2', name, self.a)
@classmethod
def foo3(cls, name):
print('hello foo3', name, cls.a)
print("hello foo5", cls().foo2(name))
print("hello foo6", cls().foo1(name))
class B(A):
a = 'b'
@staticmethod
def foo1(name):
print('subclass B, hello', name, B.a)
def foo2(self, name):
print('subclass B, hello', name, self.a)
@classmethod
def foo3(cls, name):
print('subclass B, hello', name, cls.a)
重點(diǎn)應(yīng)關(guān)注@staticmethod和@classmethod調(diào)用本類或其他類的函數(shù)和屬性的區(qū)別
例子1:
關(guān)鍵看第二句 subclass B, hello 小熊貓 b,在調(diào)用 B.foo2(B, “小熊貓”) 時(shí),執(zhí)行了B類型下的foo2()方法,該方法無返回值,因此 下句輸出為 hello foo4 None
a = A()
a.foo1("小熊貓")
# 輸出
hello foo1 小熊貓 a
subclass B, hello 小熊貓 b
hello foo4 None
例子2:
a.foo3("小熊貓")
# 輸出
hello foo3 小熊貓 a
hello foo2 小熊貓 a
hello foo5 None
hello foo1 小熊貓 a
subclass B, hello 小熊貓 b
hello foo4 None
hello foo6 None
PS:下面看下python中self和cls的區(qū)別
1、self表示一個(gè)具體的實(shí)例本身。如果用了staticmethod,那么就可以無視這個(gè)self,將這個(gè)方法當(dāng)成一個(gè)普通的函數(shù)使用。
2、cls表示這個(gè)類本身。
>>> class A(object):
def foo1(self):
print "Hello",self
@staticmethod
def foo2():
print "hello"
@classmethod
def foo3(cls):
print "hello",cls
>>> a = A()
>>> a.foo1() #最常見的調(diào)用方式,但與下面的方式相同
Hello <__main__.A object at 0x9f6abec>
>>> A.foo1(a) #這里傳入實(shí)例a,相當(dāng)于普通方法的self
Hello <__main__.A object at 0x9f6abec>
>>> A.foo2() #這里,由于靜態(tài)方法沒有參數(shù),故可以不傳東西
hello
>>> A.foo3() #這里,由于是類方法,因此,它的第一個(gè)參數(shù)為類本身。
hello <class '__main__.A'>
>>> A #可以看到,直接輸入A,與上面那種調(diào)用返回同樣的信息。
<class '__main__.A'>
3、whats more,類先調(diào)用__new__方法,返回該類的實(shí)例對(duì)象,這個(gè)實(shí)例對(duì)象就是__init__方法的第一個(gè)參數(shù)self,即self是__new__的返回值。
總結(jié)
到此這篇關(guān)于深入淺析python 中的self和cls的區(qū)別的文章就介紹到這了,更多相關(guān)python 中的self和cls內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
tensorflow2.0與tensorflow1.0的性能區(qū)別介紹
今天小編就為大家分享一篇tensorflow2.0與tensorflow1.0的性能區(qū)別介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python OOP類中的幾種函數(shù)或方法總結(jié)
今天小編就為大家分享一篇關(guān)于Python OOP類中的幾種函數(shù)或方法總結(jié),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02
對(duì)Python中數(shù)組的幾種使用方法總結(jié)
今天小編就為大家分享一篇對(duì)Python中數(shù)組的幾種使用方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06
輕松實(shí)現(xiàn)TensorFlow微信跳一跳的AI
這篇文章主要教大家如何輕松實(shí)現(xiàn)TensorFlow微信跳一跳的AI,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Python?pandas中read_csv參數(shù)示例詳解
使用pandas做數(shù)據(jù)處理的第一步就是讀取數(shù)據(jù),數(shù)據(jù)源可以來自于各種地方,csv文件便是其中之一,下面這篇文章主要給大家介紹了關(guān)于Python?pandas中read_csv參數(shù)詳解的相關(guān)資料,需要的朋友可以參考下2023-01-01

