[基础] - Python中 @staticmethod 和 @classmethod
staticmethod 基本上和一个全局函数差不多,只不过可以通过类或类的实例对象来调用而已, 不会隐式地传入任何参数。这个和静态语言中的静态方法比较像。
classmethod 是和一个class相关的方法,可以通过类或类实例调用,并将该class对象隐式地当作第一个参数传入。类方法的第一个参数是cls,而实例方法的第一个参数是self。
class Person:
def __init__(self):
print "init"
@staticmethod
def sayHello(hello):
print "staticmethod"
@classmethod
def introduce(cls,hello):
cls.sayHello(hello)
print "classmethod"