[Python-ideas] Specificity in AttributeError

Brett Cannon brett at python.org
Sat Apr 27 17:10:33 CEST 2013


On Sat, Apr 27, 2013 at 8:04 AM, Georg Brandl <g.brandl at gmx.net> wrote:
> 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.

I can speak from two bits of experience on this. First is
http://python.org/dev/peps/pep-0352/ where I tried to make
BaseException only accept a single argument and add a 'message'
attribute. I actually gave up on the single argument version because
too much called relied on the *args acceptance of BaseException. I
gave up on 'message' because too many people subclassed exceptions and
used 'message' as an attribute.

But I also added 'name' and 'path' to ImportError in Python 3.3
successfully. I made them keyword-only arguments to the module's
constructor to avoid API problems and not enough people construct
ImportError instances directly for me to have heard complaints about
the attribute names.

My point is that Georg is right: tweaking the base exceptions by even
adding an attribute can be touchy, but worth it.



More information about the Python-ideas mailing list