python静态方法和类方法

class TestStaticMethod:
    def foo():
        print 'calling static method foo()'

    foo = staticmethod(foo)

class TestClassMethod:
    def foo(a):
        print 'calling class method foo()'
        print 'foo is part of class:',a.__name__

    foo = classmethod(foo)

        
class TestStaticMethod:
    @staticmethod
    def foo():
        print 'calling static method foo()'

class TestClassMethod:
    @classmethod
    def foo(a):
        print 'calling class method foo()'
        print 'foo is part of class:',a.__name__

>>> TestStaticMethod.foo()
calling static method foo()
>>> tsm = TestStaticMethod()
>>> tsm.foo()
calling static method foo()
>>> tcm = TestClassMethod()
>>> tcm.foo()
calling class method foo()
foo is part of class: TestClassMethod
>>> ================================ RESTART ================================
>>> 
>>> tcm = TestClassMethod()
>>> tcm.foo()
calling class method foo()
foo is part of class: TestClassMethod

你可能感兴趣的:(python)