函数的参数是一个函数:

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

 

相关文章:

  • 2021-06-08
猜你喜欢
  • 2022-01-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-27
  • 2022-01-07
  • 2021-10-02
相关资源
相似解决方案