[Python-ideas] accurate errors for "magic" methods

Scott Dial scott+python-ideas at scottdial.com
Fri Apr 10 18:01:10 CEST 2009

Steven D'Aprano wrote:
> Except that the error "object is unsubscriptable" might as well be in 
> Klingon to most people, particularly newbies. 
> (1) It's easy to misread it as "unscriptable", which is even more 
> mysterious.
> (2) As far as I know, there's no tradition of describing key or index 
> lookup as "subscripting" in Python. I've never noticed it in doc 
> strings or the online docs, and after hanging around comp.lang.python 
> extensively for years, I feel safe to say it's not a common term among 
> even experienced Python developers. I suppose that there's a weak 
> connection between index lookup and subscripts in mathematics.
> (3) The classic error message tells the newbie exactly what the error 
> is: the object has no __getitem__ method. The new error message tells 
> the newbie nothing useful. Given that obj is unsubscriptable, what 
> needs to be done to make it subscriptable?

This exact topic came up almost exactly a year ago:


It seems the only thing that came out of that discussion was that set()
now has a new error message (that is still inconsistent):

Python 2.7a0 (trunk:71448, Apr 10 2009, 11:49:22)
[GCC 4.1.2 (Gentoo 4.1.2 p1.1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> set([1,2,3])[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing

Perhaps the most relevant part though is GvR saying:

I wouldn't bet my life on that. __getitem__ overloading for all sorts
of nefarious purposes has quite a following. I'd prefer a message that
doesn't get into what x "is" but sticks to the point at hand, which is
that it doesn't support __getitem__.

Scott Dial
scott at scottdial.com
scodial at cs.indiana.edu

More information about the Python-ideas mailing list