Tag: None type success Python bar item module obj Pass
Implementing adding properties to a class
def Typed (**kwargs): def deco (obj): for Key,val in Kwargs.items (): # obj.ke y = val setattr (obj,key,val) return obj print (' ==> ', Kwargs) return deco@typed (X=1,y=2,z=3) class foo:pass# f = Foo () print (foo.__dict__) #给Foo类添加属性成功 #{' __module__ ': ' __main__ ', ' __dict__ ': <attribute ' __dict__ ' O F ' foo ' objects>, ' __weakref__ ': <attribute ' __weakref__ ' of ' Foo ' objects>, ' __doc__ ': None, ' x ': 1, ' Y ': 2, ' Z ' : 3} @Typed (Name= ' ago ') class Bar:passprint (bar.__dict__) ' ==> {' x ': 1, ' Y ': 2, ' z ': 3}{' __module__ ': ' __main__ ', ' _ _dict__ ': <attribute ' __dict__ ' of ' foo ' objects>, ' __weakref__ ': <attribute ' __weakref__ ' of ' foo ' objects> ' __doc__ ': None, ' x ': 1, ' Y ': 2, ' Z ': 3}==> {' name ': ' ago '} {' __module__ ': ' __main__ ', ' __dict__ ': <attribute ' __dic t__ ' of ' bar ' objects>, ' __weakref__ ': <attribute ' __weakref__ ' of ' bar ' objects>, ' __doc__ ': None, ' name ': ' Ago ' } '
Adorner for class