一、@classmethod 和 @staticmethod
1、staticmethod
作用:让类中的方法变成一个普通函数(普通函数没有绑定在任何一个特定的类或者实例上。所以与不需要对象实例化就可以直接调用)
特点:既可以使用类直接调用,也可以类的实例调用,并且没有任何隐含参数的传入,所以不需要self(参数名是随便定的)。
>>> class C(object):
... @staticmethod
... def add(a,b):
... return a+b
... def get_weight(self):
... return self.add(1,2)
...
>>> C.add
<function add at 0x1d32668>
>>> C().add
<function add at 0x1d32668>
>>> C.get_weight
<unbound method C.get_weight>
2、classmethod
二、类中方法参数cls和self的区别