最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2018年10月11日 09:46:42   作者:斜陽雨陌  
今天小編就為大家分享一篇關(guān)于Python中staticmethod和classmethod的作用與區(qū)別,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧

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

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

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

既然@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ì)象等,避免硬編碼。

要明白,什么是實(shí)例方法、靜態(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)用的時(shí)候沒有顯式聲明cls,但實(shí)際上類本身是作為隱含參數(shù)傳入的。這就像實(shí)例方法在調(diào)用的時(shí)候也沒有顯式聲明self,但實(shí)際上實(shí)例本身是作為隱含參數(shù)傳入的。

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

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

class Fruit(object):
 total = 0 # 這是一個(gè)類屬性
 @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)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

  • Python pymysql操作MySQL詳細(xì)

    Python pymysql操作MySQL詳細(xì)

    pymysql是Python3.x中操作MySQL數(shù)據(jù)庫的模塊,其兼容于MySQLdb,使用方法也與MySQLdb幾乎相同,但是性能不如MySQLdb,但是由于其安裝使用方便、對(duì)中文兼容性也更好等優(yōu)點(diǎn),被廣泛使用??梢允褂胮ip install pymysql進(jìn)行安裝。
    2021-09-09
  • matplotlib 使用 plt.savefig() 輸出圖片去除旁邊的空白區(qū)域

    matplotlib 使用 plt.savefig() 輸出圖片去除旁邊的空白區(qū)域

    這篇文章主要介紹了matplotlib 使用 plt.savefig() 輸出圖片去除旁邊的空白區(qū)域,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Python任務(wù)調(diào)度模塊APScheduler使用

    Python任務(wù)調(diào)度模塊APScheduler使用

    這篇文章主要介紹了Python任務(wù)調(diào)度模塊APScheduler使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • 利用python和ffmpeg 批量將其他圖片轉(zhuǎn)換為.yuv格式的方法

    利用python和ffmpeg 批量將其他圖片轉(zhuǎn)換為.yuv格式的方法

    今天小編就為大家分享一篇利用python和ffmpeg 批量將其他圖片轉(zhuǎn)換為.yuv格式的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python使用Flask框架獲取當(dāng)前查詢參數(shù)的方法

    Python使用Flask框架獲取當(dāng)前查詢參數(shù)的方法

    這篇文章主要介紹了Python使用Flask框架獲取當(dāng)前查詢參數(shù)的方法,實(shí)例分析了query_string獲取查詢參數(shù)的技巧,需要的朋友可以參考下
    2015-03-03
  • python 6種方法實(shí)現(xiàn)單例模式

    python 6種方法實(shí)現(xiàn)單例模式

    這篇文章主要介紹了python 6種方法實(shí)現(xiàn)單例模式,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • python 實(shí)現(xiàn)兩個(gè)變量值進(jìn)行交換的n種操作

    python 實(shí)現(xiàn)兩個(gè)變量值進(jìn)行交換的n種操作

    這篇文章主要介紹了python 實(shí)現(xiàn)兩個(gè)變量值進(jìn)行交換的n種操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • opencv實(shí)現(xiàn)簡單人臉識(shí)別

    opencv實(shí)現(xiàn)簡單人臉識(shí)別

    這篇文章主要為大家詳細(xì)介紹了opencv實(shí)現(xiàn)簡單人臉識(shí)別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • python光學(xué)仿真面向?qū)ο蠊鈱W(xué)元件類的實(shí)現(xiàn)

    python光學(xué)仿真面向?qū)ο蠊鈱W(xué)元件類的實(shí)現(xiàn)

    這篇文章主要為大家介紹了python光學(xué)仿真面向?qū)ο蠊鈱W(xué)元件類的實(shí)現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-10-10
  • Python利用lxml模塊爬取豆瓣讀書排行榜的方法與分析

    Python利用lxml模塊爬取豆瓣讀書排行榜的方法與分析

    這篇文章主要給大家介紹了關(guān)于Python爬蟲利用lxml模塊爬取豆瓣讀書排行榜的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評(píng)論

鄯善县| 延川县| 兰溪市| 广灵县| 赤峰市| 英德市| 平武县| 肃南| 上杭县| 金华市| 云梦县| 崇仁县| 息烽县| 韩城市| 漾濞| 连云港市| 安泽县| 武鸣县| 铜山县| 莱芜市| 墨竹工卡县| 庄河市| 堆龙德庆县| 大化| 濮阳县| 安远县| 丹江口市| 湖北省| 太湖县| 濉溪县| 京山县| 乌兰县| 涪陵区| 和田市| 若羌县| 宝应县| 中阳县| 上蔡县| 突泉县| 竹山县| 白沙|