Issue 5760: getitem error message hard to understand (original) (raw)

Prompted by http://mail.python.org/pipermail/python-ideas/2009-April/004048.html

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 understand:

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

Problems observed:

Suggestion: 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.

As I said on the python-ideas discussion, which definitely did not come to consensus, I disagree with this suggestion. To repeat and expand on what I said there:

  1. 'unsubscriptable' could instead be changed to 'not subscriptable'.

  2. 'subscription' is the way Python describes the use of []. """A subscription selects an item of a sequence (string, tuple or list) or mapping (dictionary) object:

subscription ::= primary "[" expression_list "]" """ 'array subscript' (from C, for example) is a standard term used for decades in computer languages. It is appropriate since a[i] is one standard single-text-line replacement for ai when one cannot actually 'type' a subscript. It is a way of representing a subscript, just as i**j is an in-line way of representing a superscript exponent, which we still call an exponent in spite of it not being 'raised'. Both constitute visible markup that is part of the compromise in representing 2D typography with a 1D stream of characters. Using '/' for division (and fractions) is another.

  1. Python error message generally do not and I think should not mention the special method implementation that underlies surface level errors. In this particular case, the remedy to mistakenly trying to subscript something is to not do that. I think mentioning getitem would be a distraction. One generally cannot just go around adding it.