双下划线开头的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)

 

posted @ 2019-09-18 15:11  汉魂县令  阅读(35)  评论(0)    收藏  举报