Python中staticmethod和classmethod的作用與區(qū)別
一般來(lái)說(shuō),要使用某個(gè)類(lèi)的方法,需要先實(shí)例化一個(gè)對(duì)象再調(diào)用方法。
而使用@staticmethod或@classmethod,就可以不需要實(shí)例化,直接類(lèi)名.方法名()來(lái)調(diào)用。
這有利于組織代碼,把某些應(yīng)該屬于某個(gè)類(lèi)的函數(shù)給放到那個(gè)類(lèi)里去,同時(shí)有利于命名空間的整潔。
既然@staticmethod和@classmethod都可以直接類(lèi)名.方法名()來(lái)調(diào)用,那他們有什么區(qū)別呢
從它們的使用上來(lái)看
- @staticmethod不需要表示自身對(duì)象的self和自身類(lèi)的cls參數(shù),就跟使用函數(shù)一樣。
- @classmethod也不需要self參數(shù),但第一個(gè)參數(shù)需要是表示自身類(lèi)的cls參數(shù)。
如果在@staticmethod中要調(diào)用到這個(gè)類(lèi)的一些屬性方法,只能直接類(lèi)名.屬性名或類(lèi)名.方法名。
而@classmethod因?yàn)槌钟衏ls參數(shù),可以來(lái)調(diào)用類(lèi)的屬性,類(lèi)的方法,實(shí)例化對(duì)象等,避免硬編碼。
要明白,什么是實(shí)例方法、靜態(tài)方法和類(lè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)
雖然類(lèi)方法在調(diào)用的時(shí)候沒(méi)有顯式聲明cls,但實(shí)際上類(lèi)本身是作為隱含參數(shù)傳入的。這就像實(shí)例方法在調(diào)用的時(shí)候也沒(méi)有顯式聲明self,但實(shí)際上實(shí)例本身是作為隱含參數(shù)傳入的。
對(duì)于靜態(tài)函數(shù),我們一般把與類(lèi)無(wú)關(guān)也與實(shí)例無(wú)關(guān)的函數(shù)定義為靜態(tài)函數(shù)。例如入口檢查的函數(shù)就最好定義成靜態(tài)函數(shù)。
類(lèi)方法的妙處, 在繼承中的作用:
class Fruit(object):
total = 0 # 這是一個(gè)類(lèi)屬性
@classmethod
def print_total(cls):
print('this is the ', cls, '.total:', cls.total, ' and its id: ', id(cls.total)) # cls是類(lèi)本身,打印類(lèi)屬性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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- python中的classmethod與staticmethod
- 詳解Python中@staticmethod和@classmethod區(qū)別及使用示例代碼
- Python 類(lèi)方法和實(shí)例方法(@classmethod),靜態(tài)方法(@staticmethod)原理與用法分析
- python的staticmethod與classmethod實(shí)現(xiàn)實(shí)例代碼
- 基于python中staticmethod和classmethod的區(qū)別(詳解)
- Python?class類(lèi)@staticmethod及@classmethod區(qū)別淺析
相關(guān)文章
導(dǎo)致python中import錯(cuò)誤的原因是什么
在本篇文章里小編給大家整理了關(guān)于python的import錯(cuò)誤原因以及相關(guān)內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2020-07-07
Python通過(guò)matplotlib繪制動(dòng)畫(huà)簡(jiǎn)單實(shí)例
這篇文章主要介紹了Python通過(guò)matplotlib繪制動(dòng)畫(huà)簡(jiǎn)單實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
Centos7 Python3下安裝scrapy的詳細(xì)步驟
這篇文章主要介紹了Centos7 Python3下安裝scrapy的詳細(xì)步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
利用Python小工具實(shí)現(xiàn)3秒鐘將視頻轉(zhuǎn)換為音頻
這篇文章主要介紹了利用Python小工具實(shí)現(xiàn) 3秒鐘將視頻轉(zhuǎn)換為音頻效果,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
快速解決jupyter notebook啟動(dòng)需要密碼的問(wèn)題
這篇文章主要介紹了快速解決jupyter notebook啟動(dòng)需要密碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
解決Djang2.0.1中的reverse導(dǎo)入失敗的問(wèn)題
今天小編就為大家分享一篇解決Djang2.0.1中的reverse導(dǎo)入失敗的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08

