yanfei 发表于 2017-5-1 11:49:10

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
页: [1]
查看完整版本: python静态方法和类方法