python面向对象--类的装饰器
# def deco(obj): # print("=====",obj) # obj.x=1 # return obj # @deco#===> test = deco(test)----->return func # def test(): # print("test函数运行") # # test() def deco(obj): print("=====",obj) obj.x=1 obj.y=2 return obj @deco # Foo = deco(Foo) class Foo(): pass print(Foo.__dict__) #一切皆对象 def test(): print("test") test.x=1 print(test.__dict__) def typed(**kwargs): def deco(obj): print("======>",kwargs) print("=====>类名",obj) for key,val in kwargs.items(): # obj.key=val setattr(obj,key,val) return obj print("===>",kwargs) return deco @typed(x=1,y=2,z=3)#1.typed(x=1,y=2,z=3)--->deco 2.@deco---->foo=deco(foo) class Foo: pass print(Foo.__dict__) @typed(name="egon")# @deco===>Bar=deco(Bar) class Bar: pass print(Bar.name)
如果我失败了,至少我尝试过,不会因为痛失机会而后悔