`
san_yun
  • 浏览: 2639419 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

AttributeError: Foo instance has no attribute '__getitem__'

 
阅读更多

一个object是不能像dict那样通过[]访问的,比如下面这段代码会报错:

>>> class Foo():
...     id = 1
... 
>>> 
>>> f = Foo()
>>> f[id]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Foo instance has no attribute '__getitem__'

 正确的做法是通过getattr(obj, name)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics