标签:
class Dict(dict): def __getattr__(self,key): return self[key] def __setattr__(self,key,value): dict.__setitem__(self,key,value) def __delattr__(self,key): dict.__delitem__(self,key) if __name__==‘__main__‘: d=Dict(a=1,b=2) print d.a #通过d.a获取d[‘a‘] d.a=2 #通过d.a修改d[‘a‘] print d[‘a‘] del d.a #通过d.a删除d[‘a‘] try: print d[a] except NameError as e: print e
__getattr__:
element 是实例对象(在 t.__dict__中),t.element不会去寻找 __getattr__
真是天坑,看不懂 http://blog.csdn.net/huithe/article/category/645491
__getattr__,__setattr__,__delattr__
标签:
原文地址:http://www.cnblogs.com/Citizen/p/4757917.html