TypeError: object cannot be interpreted as an index

Peter Otten __peter__ at web.de
Wed Apr 8 11:27:00 CEST 2009


tleeuwenburg at gmail.com wrote:

> It started with this error message... "TypeError: object cannot be
> used as an index"
> 
> foo = {}
> someObject = someClass()
> foo[someObject] = "hello"

"interpreted" or "used"? 

If the former, 'foo' may be a list rather than a dict, and someClass a
classic class:

>>> class A: pass
...
>>> {A(): 42}
{<__main__.A instance at 0x2ac2a6d35bd8>: 42}
>>> [][A()]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object cannot be interpreted as an index

To find out the actual the actual type of a classic class look into the
__class__ attribute:

>>> type(A())
<type 'instance'>
>>> A().__class__
<class __main__.A at 0x2ac2a6d1e830>

Peter

PS: Please cut and paste in the future.




More information about the Python-list mailing list