Python中@staticmethod和@classmethod之间的区别
@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)
| Takes cls as first parameter | Needs no specific parameters |
| Can access or modify the class state | Cannot access the class state |
| They must have parameters | Knows nothing about the class state. Are similar to utility methods. |
| 以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)題。
- 上一篇: 算法中的Strassen矩阵乘法
- 下一篇: 绝密543剧情介绍