日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

Python中@staticmethod和@classmethod之间的区别

發(fā)布時(shí)間:2023/12/1 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python中@staticmethod和@classmethod之间的区别 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

@classmethod裝飾器 (The @classmethod Decorator)

The @classmethod decorator is an inbuilt function decorator that gets evaluated after the function is defined. The result of the evaluation shadows the function definition. The @classmethod's first argument is always a class cls, similar to an instance method receiving self as its first argument.

@classmethod裝飾器是一個(gè)內(nèi)置的函數(shù)裝飾器,在定義函數(shù)后會(huì)對(duì)其進(jìn)行評(píng)估。 評(píng)估結(jié)果遮蓋了功能定義。 @classmethod的第一個(gè)參數(shù)始終是cls類(lèi),類(lèi)似于將self作為其第一個(gè)參數(shù)的實(shí)例方法。

Syntax:

句法:

Class ABC(object):@classmethoddef function(cls, arg1, ...):...
  • Exists to create class methods that are passed with the actual class object within the function call.

    存在以創(chuàng)建在函數(shù)調(diào)用中與實(shí)際類(lèi)對(duì)象一起傳遞的類(lèi)方法。

  • Bound to the class and not to an instance.

    綁定到類(lèi)而不是實(shí)例。

  • Can modify the class state and that would be applied across all the instances.

    可以修改類(lèi)狀態(tài),并將其應(yīng)用于所有實(shí)例。

@staticmethod裝飾器 (The @staticmethod Decorator)

@staticmethods, similar to class methods, are methods that are bound to class rather than its object. However, they do not require a class instance creation. So, are not dependent on the state of the object.

與類(lèi)方法類(lèi)似, @staticmethods是綁定到類(lèi)而不是對(duì)象的方法。 但是,它們不需要?jiǎng)?chuàng)建類(lèi)實(shí)例。 因此,不依賴(lài)于對(duì)象的狀態(tài)。

Syntax:

句法:

Class ABC(object):@staticmethoddef function(arg1, arg2, ...):...
  • Bound to the class and not to an instance

    綁定到類(lèi)而不是實(shí)例

  • Cannot modify the class state

    無(wú)法修改類(lèi)狀態(tài)

@classmethod和@staticmethod之間的比較 (Comparison between @classmethod and @staticmethod)

Class methodStatic method
Takes cls as first parameterNeeds no specific parameters
Can access or modify the class stateCannot access the class state
They must have parametersKnows nothing about the class state. Are similar to utility methods.
類(lèi)方法 靜態(tài)方法
以cls作為第一個(gè)參數(shù) 不需要特定參數(shù)
可以訪問(wèn)或修改類(lèi)狀態(tài) 無(wú)法訪問(wèn)類(lèi)狀態(tài)
他們必須有參數(shù) 對(duì)類(lèi)狀態(tài)一無(wú)所知。 與實(shí)用程序方法相似。

@classmethod和@staticmethod的示例實(shí)現(xiàn) (Example implementation of @classmethod and @staticmethod)

class City: def __init__(self, zip_code, name): self.zip_code = name self.name = name # a class method to create a city object. @classmethoddef city_name(cls, zip_code, name): return cls(zip_code, name) # a static method to check if a city is capital or not@staticmethoddef isCapital(city_name): if city_name == 'bengaluru':return Trueif __name__ == '__main__':bengaluru = City(560086, 'bengaluru')mysuru = City.city_name(560111, 'mysuru')print("city is {}".format(bengaluru.name))print("city is {}".format(mysuru.name))print("Bengaluru is capital :{}".format(City.isCapital('bengaluru')))

Output

輸出量

city is bengaluru city is mysuru Bengaluru is capital : True

翻譯自: https://www.includehelp.com/python/staticmethod-vs-classmethod.aspx

總結(jié)

以上是生活随笔為你收集整理的Python中@staticmethod和@classmethod之间的区别的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。