我正在尝试制作一个 VarDump 类,我可以在其中执行以下操作:
vd.attribute = value
和
print vd.attribute
所以,这是代码:
class VarDump:
def __init__(self):
self.dump={}
def __setattr__(self,item,var):
self.dump[item]=var
def __getattr__(self,item):
if not item in self.dump: return ""
return self.dump[item]
vd = VarDump()
vd.foo="bar"
print vd.foo
但我收到了这个错误:
File "classAsVarDump.py", line 9, in __getattr__
if not item in self.dump: return ""
RuntimeError: maximum recursion depth exceeded