Python内置函数之classmethod()

函数的参数是一个函数:

classmethod(func)

作用是,在外部,类对象能够直接调用类方法。

常用来作为装饰器。

>>> class C:
...   def f(self):
...     print('ok')
...
>>> C.f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() missing 1 required positional argument: 'self'
>>> class C:
...   @classmethod
...   def f(self):
...     print('ok')
...
>>> C.f()
ok
原文地址:https://www.cnblogs.com/leomei91/p/7353605.html