【发布时间】:2011-03-19 09:35:04
【问题描述】:
这是我的代码:
class fun:
def __getattr__(self,key):
return self[key]
def __setattr__(self,key,value):
self[key] = value+1
a = fun()
a['x']=1
print a['x']
错误是:
AttributeError: fun instance has no attribute '__getitem__'
当我把它改成:
class fun:
def __getattr__(self,key):
return self.key
def __setattr__(self,key,value):
self.key = value+1
a = fun()
a.x=1
print a.x
错误是:
RuntimeError: maximum recursion depth exceeded
我该怎么办,我想得到2
【问题讨论】: