Author cvrebert
Recipients cvrebert
Date 2009-04-15.06:32:09
SpamBayes Score 7.91569e-06
Marked as misclassified No
Message-id <>
Prompted by

The current error message issued when trying to use the get item ([])
operator on an object that does not define __getitem__ can be hard to

>>> class A(object): pass
>>> A()['a']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'A' object is unsubscriptable

Problems observed:
- "unsubscriptable" is easily misread in haste as "unscriptable", which
can be quite confusing, especially to newbies
- "subscripting" is not frequently used to describe the [] operator,
making the message more difficult to decipher (again, esp. for newbies)
- the underlying lack of a __getitem__ method is not mentioned, thus not
making it obvious how to remedy the error

Use exception chaining and rephrase the error message to get something like:

AttributeError: class 'A' has no attribute '__getitem__'
The above exception was the direct cause of the following exception:
TypeError: 'A' object does not support the 'get item' operator

Similar changes should be made to __setitem__ & __delitem__.
Date User Action Args
2009-04-15 06:32:12cvrebertsetrecipients: + cvrebert
2009-04-15 06:32:11cvrebertsetmessageid: <>
2009-04-15 06:32:10cvrebertlinkissue5760 messages
2009-04-15 06:32:09cvrebertcreate