Menu Close

Python 内置函数 classmethod( )

把一个方法封装成类方法。

类方法隐含的第一个参数就是类,就像实例方法接收实例作为参数一样。要声明一个类方法,按惯例请使用以下方案:

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

语法:

classmethod(function)

classmethod() 被认为是非 Python 的,因此在较新的 Python 版本中,您可以使用 @classmethod 装饰器来定义类方法。

语法:

@classmethod 
def func(cls, args...)

classmethod() 参数

classmethod() 方法采用单个参数:

  • function – 需要转换为类方法的函数

classmethod() 返回值

classmethod() 方法返回给定函数的类方法。

(草稿)

wer

除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Leave the field below empty!

Posted in Python教程

Related Posts