首頁  >  文章  >  後端開發  >  python函數 - classmethod()

python函數 - classmethod()

高洛峰
高洛峰原創
2016-10-17 15:31:011421瀏覽

classmethod(function)

中文說明:

classmethod是用來指定一個類別的方法為類別方法,沒有此參數指定的類別的方法為實例方法,使用方法如下:

class C:
    @classmethod
    def f(cls, arg1, arg2, ...): ...

   

類既可以直接類別呼叫(C.f()),也可以進行實例呼叫(C().f())。

版本:python2.2新增,在python2.4中增加新功能。 python3中仍可使用。


英文說明:

Return a class method for function.

A class method receives the class as implicit first argument, justike lri col 完成

class C:
    @classmethod
    def f(cls, arg1, arg2, ...): ...

   

The @classmethod form is a function decorator – see the description of function definitions in Function definitions for details.

) 🜒 class class class class。 on an instance (such as C().f()). The instance is ignored except for its class. If a class method is called for a derived class, the derived class object is passed as the implied first argument.🜟 Class methods are different than C++ or Java static methods. If you want those, see staticmethod() in this section.


For more information on class types, cons

the hemage the hah​​hentation on class 類型, cons


New in version 2.2.


Changed in version 2.4: Function decorator syntax


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn