[Python-ideas] Specificity in AttributeError

Georg Brandl g.brandl at gmx.net
Sat Apr 27 14:04:58 CEST 2013


Am 27.04.2013 13:53, schrieb Ned Batchelder:

>>> hasattr_lite (if it worked) would've reported the problem as an
>>> AttributeError, with exactly the typo I had made in an attribute name
>>> inside a property. Seconds to figure out and fix.
>>>
>>> So what I mean is, it isn't necessary, but I would find it helpful and
>>> convenient.
>>
>> I absolutely agree, and would like to see the available info on other 
>> exceptions (KeyError, IndexError, etc.) as well. I suspect it would 
>> take some serious effort to upgrade all the exceptions from all the 
>> places they can be raised from, though.
> 
> I also agree that more information can only be a good thing.  Unless 
> someone can show why it could be harmful (cycles caused by the exception 
> keeping a reference to the offending object??), the only downside I can 
> see is the work needed to change the throw points.

It is kind of harmful to duck-typing and, to a lesser degree, inheritance:
so far Python has never guaranteed anything about exception arguments.
If the exception attributes become part of the interface of standard types,
everyone implementing a replacement will have to conform (and there are
lots and lots of such replacements out there).  This change should be
treated akin to adding a new method to dictionaries, for example.

That said, personally I would be in favour of such a change, because the
advantage for unhandled exceptions alone is significant.

Georg




More information about the Python-ideas mailing list