日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python classmothed_python函数之classmethod()

發布時間:2025/4/16 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python classmothed_python函数之classmethod() 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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, just like an instance method receives the instance. To declare a class method, use this idiom: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.

It can be called either on the class (such as C.f()) or 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 methods, consult the documentation on the standard type hierarchy in The standard type hierarchy.

New in version 2.2.

Changed in version 2.4: Function decorator syntax added.

代碼實例:>>> class C:

... @classmethod

... def f(self):

... print "This is a class method"

...

>>> C.f()

This is a class method

>>> c = C()

>>> c.f()

This is a class method

>>> class D:

... def f(self):

... print " This is not a class method "

...

>>> D.f()

Traceback (most recent call last):

File "", line 1, in

TypeError: unbound method f() must be called with D instance as first argument (got nothing instead)

>>> d = D()

>>> d.f()

This is not a class method

總結

以上是生活随笔為你收集整理的python classmothed_python函数之classmethod()的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。