how to get the current thing in this code
this is my code :
class a():
def __init__(self):
self.a="aaa"
self.b='bbb'
aa=a()
x=['a','b','c']
for i in x:
print aa.get(i)
it show error :
Traceback (most recent call last):
File "a.py", line 10, in <module>
print aa['a']
AttributeEr开发者_StackOverflow社区ror: a instance has no attribute '__getitem__'
what can i do ,
thanks
updated:
i want print 'aaa' and 'bbb'
Perhaps you are looking for getattr(aa, i)
?
You'll have to use the getattr
function or define a __getitem__
method for class a
(though to implement it, you'll probably end up using getattr
).
It looks like the error message was generated by different code where you were trying aa[i]
rather than aa.get(i)
, which means (besides that the sample isn't representative) defining a.__getattr__
won't work with the sample code, though it would work if you use aa[i]
instead of aa.get(i)
.
精彩评论