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

Terry Reedy tjreedy at udel.edu
Sat Apr 11 00:31:03 CEST 2009


Steven D'Aprano wrote:
>
> 
> (1) It's easy to misread it as "unscriptable", which is even more 
> mysterious.

'Cannot be subscripted' might be clearer.

> (2) As far as I know, there's no tradition of describing key or index 
> lookup as "subscripting" in Python....

Reference / Expressions

"Subscriptions
A subscription selects an item of a sequence (string, tuple or list) or 
mapping (dictionary) object:..."

> (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.

It says "Do not try to subscript this object'.

 > Given that obj is unsubscriptable, what
> needs to be done to make it subscriptable?

Typicallly, nothing.  Usually, there is no __getitem__ because there 
should not be.  How would you make a number subscriptable?

To me, saying 'Int cannot be subscripted' (don't do it) is more helpful 
to newbies than 'Int has no __getitem__ method' (so it cannot be 
subscripted, so stop trying).

tjr




More information about the Python-ideas mailing list