一切皆对象:
def test():
print()
test.x=1
test.y=2
print(test.__dict__)
float 浮点类型
def dec(obj): print('123456') obj.x=1 obj.y=2 obj.z=3 return obj @dec #Foo=dec(Foo) class Foo: pass print(Foo.__dict__)
def Typed(**kwargs): def dec(obj): for k,y in kwargs.items(): setattr(obj,k,y) # obj.__dict__.update(kwargs) return obj return dec @Typed(x=1,y=2) class Foo: pass print(Foo.__dict__)
class Foo: def __init__(self,key,expect_type): self.key=key self.expect_type=expect_type def __set__(self, instance, value): print('set方法') if not isinstance(value,self.expect_type): raise TypeError('错误') instance.__dict__[self.key]=value def __delete__(self, instance): print('delete方法') def __get__(self, instance, owner): print('get方法') def Typed(**kwargs): def dec(obj): for k,y in kwargs.items(): setattr(obj,k,Foo(k,y)) # obj.__dict__.update(kwargs) return obj return dec @Typed(name=str,age=int) class Too: # name=Foo('name',str) # age=Foo('age',int) def __init__(self,name,age): self.name=name self.age=age t1=Too('林南',29) t1.name print(t1.__dict__)