day25 Python __setattr__

#__getattr__只有在使用点调用属性且属性不存在的时候才会触发

class Foo:
    x=1
    def __init__(self,y):
        self.y=y
    def __getattr__(self, item):
        print('----> from getattr:你找的属性不存在')
f1=Foo('10')
print(f1.y)
print(f1.x)
print(f1.iyiu)

结果:
10
1
----> from getattr:你找的属性不存在
None

猜你喜欢

转载自www.cnblogs.com/charon2/p/10442522.html
今日推荐