码迷,mamicode.com
首页 > 编程语言 > 详细

Python内置函数之classmethod()

时间:2017-08-13 16:21:13      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:mos   line   python   ...   ssi   方法   eth   file   error:   

函数的参数是一个函数:

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

 

Python内置函数之classmethod()

标签:mos   line   python   ...   ssi   方法   eth   file   error:   

原文地址:http://www.cnblogs.com/leomei91/p/7353605.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!