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

 

posted @ 2018-09-13 13:57  道高一尺  阅读(277)  评论(0编辑  收藏  举报