
正文
classmethod作用
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
>>> class A(object):
bar = 1
def func1(self):
print 'foo'>>> class A(object):
bar = 1
def func1(self):
print 'foo'
@classmethod
def func2(cls):
print 'func2'>>> A.func2()#不需要实例化
func2
>>> A.func2
<bound method type.func2 of <class '__main__.A'>>>>> A.func1()Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
A.func1()
TypeError: unbound method func1() must be called with A instance as first argument (got nothing instead)
>>> A.bar
1






