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

Daniel Fetchinson fetchinson at googlemail.com
Fri Apr 10 01:16:19 EDT 2009


>> > Actually, I'm wrong: it's perfectly clear as long as the programmer
>> > is able to follow all the necessary reflexion path; then probably
>> > also able to solve the problem without any help from python.
>> >
>> > The issue here is that a very specific (and meaningful) case
>> > (dict-like behaviour missing) is adressed using a very generic (and
>> > thus helpless) message (attributeError).
>> >
>> > I think error cases about "magic" methods, that implement
>> > conceptually meaningful behaviours, should have appropriate
>> > messages. In the case above, maybe something like: "Values instance
>> > is not an item container (no __getitem__ method found)."
>>
>> The time machine strikes again:
>> >>> class A(object): pass
>>
>> ...
>>
>> >>> A()['a']
>>
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> TypeError: 'A' object is unsubscriptable
>>
>>
>> (the difference being that A is new-style, while Values is
>> old-style.)
>
>
> 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?

Wait a minute!

(1) __getitem__ can easily be confused with __gotitem__
(2) __gotitem__ is not far from __gotitom__
(3) which an unsuspecting newbie might interpret as __got it, Tom!__
(4a) in case the newbie is called Tom, he will only assume 'it' refers
to his mind, i.e. will naturally conclude the error message is the
evil way of the python interpreter that it managed to take control of
his mind because what else can these underscores mean than a scary
loud muhahahahhahahha accompanied by a 5 seconds tune by a classic
orchestra? He will either switch off his computer or cut his own veins
with a nearby USB plug.
(4b) in case the newbie is not called Tom, well, it's better not even
thinking about this option

So you just be careful!

Cheers,
Daniel


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown



More information about the Python-list mailing list