溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python中staticmethod和classmethod的作用與區(qū)別

發(fā)布時間:2020-10-12 09:24:05 來源:腳本之家 閱讀:229 作者:斜陽雨陌 欄目:開發(fā)技術(shù)

一般來說,要使用某個類的方法,需要先實例化一個對象再調(diào)用方法。

而使用@staticmethod或@classmethod,就可以不需要實例化,直接類名.方法名()來調(diào)用。

這有利于組織代碼,把某些應該屬于某個類的函數(shù)給放到那個類里去,同時有利于命名空間的整潔。

既然@staticmethod和@classmethod都可以直接類名.方法名()來調(diào)用,那他們有什么區(qū)別呢

從它們的使用上來看

  • @staticmethod不需要表示自身對象的self和自身類的cls參數(shù),就跟使用函數(shù)一樣。
  • @classmethod也不需要self參數(shù),但第一個參數(shù)需要是表示自身類的cls參數(shù)。

如果在@staticmethod中要調(diào)用到這個類的一些屬性方法,只能直接類名.屬性名或類名.方法名。

而@classmethod因為持有cls參數(shù),可以來調(diào)用類的屬性,類的方法,實例化對象等,避免硬編碼。

要明白,什么是實例方法、靜態(tài)方法和類方法:

class Demo(object):
 def instance_method(self, your_para):
 """
 this is an instance_method
 you should call it like the follow:
 a = Demo()
 a.instance_method(your_para)
 plus: in python, we denote 'cls' as latent para of Class
 while 'self' as latent para of the instance of the Class
 :param your_para: 
 :return: 
 """
 print("call instance_method and get:", your_para)
 @classmethod
 def class_method(cls, your_para):
 """
 this is a class_method
 you can call it like the follow:
 method1:
 a = Demo()
 a.class_method(your_para)
 method2:
 Demo.class_method
 plus: in python, we denote 'cls' as latent para of Class
 while 'self' as latent para of the instance of the Class
 :param your_para: 
 :return: 
 """
 print("call class_method and get:", your_para)
 @staticmethod
 def static_method(your_para):
 """
 this is a static_method and you can call it like the 
 methods of class_method
 :param your_para: 
 :return: 
 """
 print("call static_method and get:", your_para)

雖然類方法在調(diào)用的時候沒有顯式聲明cls,但實際上類本身是作為隱含參數(shù)傳入的。這就像實例方法在調(diào)用的時候也沒有顯式聲明self,但實際上實例本身是作為隱含參數(shù)傳入的。

對于靜態(tài)函數(shù),我們一般把與類無關(guān)也與實例無關(guān)的函數(shù)定義為靜態(tài)函數(shù)。例如入口檢查的函數(shù)就最好定義成靜態(tài)函數(shù)。

類方法的妙處, 在繼承中的作用:

class Fruit(object):
 total = 0 # 這是一個類屬性
 @classmethod
 def print_total(cls):
 print('this is the ', cls, '.total:', cls.total, ' and its id: ', id(cls.total)) # cls是類本身,打印類屬性total的值
 print('this is the Fruit.total:', Fruit.total, 'and its id: ', id(Fruit.total))
 print("=======================")
 @classmethod
 def set(cls, value):
 cls.total = value
class Apple(Fruit):
 pass
class Orange(Fruit):
 pass
app1 = Apple()
app1.set(10)
app1.print_total()
Apple.print_total()
Fruit.set(2)
app1.print_total()
Fruit.print_total()
"""
output:
this is the <class '__main__.Apple'> .total: 10 and its id: 1355201264
this is the Fruit.total: 0 and its id: 1355200944
=======================
this is the <class '__main__.Apple'> .total: 10 and its id: 1355201264
this is the Fruit.total: 0 and its id: 1355200944
=======================
this is the <class '__main__.Apple'> .total: 10 and its id: 1355201264
this is the Fruit.total: 2 and its id: 1355201008
=======================
this is the <class '__main__.Fruit'> .total: 2 and its id: 1355201008
this is the Fruit.total: 2 and its id: 1355201008
=======================
"""

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對億速云的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI