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
相關文章
相關標籤/搜索