麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Python > 正文

Python中staticmethod和classmethod的作用與區別

2020-02-15 23:07:56
字體:
來源:轉載
供稿:網友

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

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

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

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

從它們的使用上來看

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

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

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

要明白,什么是實例方法、靜態方法和類方法:

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)

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

對于靜態函數,我們一般把與類無關也與實例無關的函數定義為靜態函數。例如入口檢查的函數就最好定義成靜態函數。

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

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 = valueclass Apple(Fruit): passclass Orange(Fruit): passapp1 = 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: 1355201264this is the Fruit.total: 0 and its id: 1355200944=======================this is the <class '__main__.Apple'> .total: 10 and its id: 1355201264this is the Fruit.total: 0 and its id: 1355200944=======================this is the <class '__main__.Apple'> .total: 10 and its id: 1355201264this is the Fruit.total: 2 and its id: 1355201008=======================this is the <class '__main__.Fruit'> .total: 2 and its id: 1355201008this is the Fruit.total: 2 and its id: 1355201008======================="""            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久精品视频69 | 摸逼逼视频 | 欧美wwwsss9999 | 国产毛片网 | 精品一区二区久久久久 | 精品国产一区二区三区四 | 午夜视频久久久 | 亚洲精品日韩色噜噜久久五月 | 一区二区精品视频 | 中文字幕极速在线观看 | 日本一级黄色大片 | 鲁人人人鲁人人鲁精品 | 日韩欧美中文字幕视频 | 免费国产视频大全入口 | 最新av网址在线观看 | 91性高湖久久久久久久久网站 | 一区二区久久精品66国产精品 | 91久久极品少妇韩国 | 久久午夜免费视频 | 艹男人的日日夜夜 | 亚洲成人欧美 | 日韩一级毛毛片 | 牛牛碰在线视频 | 高颜值美女啪啪 | 午夜视频在线免费观看 | 午夜在线观看视频网站 | 男人午夜视频 | 日韩黄色片免费看 | 国产成视频在线观看 | 精品国产91久久久久久久妲己 | 91 免费视频 | 久久成人午夜视频 | 97超碰资源站 | 欧美一级免费在线观看 | 色99久久 | 国产福利不卡一区二区三区 | 日韩精品中文字幕一区 | 久久美女色视频 | 精品国产一区二区三区在线观看 | chinesexxx少妇露脸 | 国产精品成人一区 |