In [48]: class TestStaticMethod():
....: @staticmethod
....: def foo():
....: print'static foo is called'
....: @classmethod
....: def foo2(cls):
....: print'class foo2 is called'
....: print'foo2 is part of calss',cls.__name__
....:
In [49]: Te
Templates/ TestStaticMethod
In [49]: TestStaticMethod.foo()
static foo is called
In [50]: TestStaticMethod.foo2()
class foo2 is called
foo2 is part of calss TestStaticMethod
In [51]: test = TestStaticMethod()
In [52]: test.foo()
static foo is called
In [53]: test.foo2()
class foo2 is called
foo2 is part of calss TestStaticMethod
總結(jié):
1 三種方法睦裳,除了實(shí)例方法只能被實(shí)例調(diào)用外造锅,其余對(duì)于類和實(shí)例均可以被調(diào)用
2 實(shí)例方法第一個(gè)參數(shù)self
靜態(tài)方法對(duì)參數(shù)無(wú)要求
類方法的第一個(gè)參數(shù)為類,一般用cls表示