1 class People(object): 2 color = ‘yellow‘ -->类的全局静态属性 3 def __init__(self,name,age): 4 self.name = name -->类的动态属性 5 self.age = age 6 7 @classmethod -->装饰器版本(talk = classmothod(talk) ) --->动态类方法 8 def talk(self): -->类的方法 9 print(‘%s want to talk‘ % self.name ) 10 11 @staticmethod -->装饰器版本(say = classmothod(say) ) ---->静态类方法 12 def say(): -->类的方法 13 print(‘%s want to say‘ % People.color ) 14 15 #函数版本 和装饰器版本效果相同 16 talk = classmethod(talk) 17 say = staticmethod(say) 18 19 调用方法: 20 People.talk() 21 People.say()
原文:http://www.cnblogs.com/dachenzi/p/6701727.html