class F(object):
def __init__(self):
self.name = 'A'
def __getattr__(self, item):
if item == 'age':
return 40
else:
raise AttributeError('没有这个属性')
f = F()
print(f.age)
class F(object):
def __setattr__(self, key, value):
self.__dict__[key] = value
a = F()
a.name = 'alex'
print(a.name)
print(a.__dict__)
原文:https://www.cnblogs.com/an5456/p/11738291.html