双下划线开头的attr方法
class Foo: x=1 def __init__(self,y): self.y=y def __getattr__(self, item): print('执行_gatattr_') f1=Foo(100) print(f1.y) f1.dgfshfg #条用不存在的对象属性是挥之执行——getattr—— class Foo: x=1 def __init__(self,y): self.y=y def __delattr__(self, item): print('执行__delattr__') f1=Foo(10) del f1.y del f1.x class Foo: x=1 def __init__(self,y): self.y=y def __setattr__(self, key, value): print('执行__setattr__') self.__dict__[key]=value f1=Foo(10) f1.t=100 print(f1.__dict__) print(f1.t)

浙公网安备 33010602011771号