# coding=utf-8
# python2
class A(object):
def foo(self, x):
print("executing foo(%s,%s)" % (self, x))
print('self:', self)
@classmethod
def class_foo(cls, x):
print("executing class_foo(%s,%s)" % (cls, x))
print('cls:', cls)
@staticmethod
def static_foo(x): # 不用传递参数self或者cls,跟静态函数形式一样
print("executing static_foo(%s)" % x)
a = A()
A.class_foo("hei") # executing class_foo(<class '__main__.A'>,hei) # ('cls:', <class '__main__.A'>)
a.class_foo("ha") # executing class_foo(<class '__main__.A'>,ha) # ('cls:', <class '__main__.A'>)
A.static_foo("xi") # executing static_foo(xi) a.static_foo("ha") # executing static_foo(ha)
# 总结:不管是@classmethod还是@staticmethod,两者意义不一样,但是都可以用类对象或者实例对象调用