[Python-ideas] Specificity in AttributeError

Devin Jeanpierre jeanpierreda at gmail.com
Sat Apr 27 08:59:01 CEST 2013


On Sat, Apr 27, 2013 at 1:49 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
> I think you "A().foo" and not "A().y" above.

Oops. Sorry.

>
>
>> The problem is that this may disguise other issues in one's code.
>
>
> Like bugs?  ;)

Yes.

> While it's always nice to have extra info in exceptions, why are you coding
> against bugs?
>
> If this is your own code you should have unit tests to catch such things.
>
> If this is someone else's code... well, it's their bug.

Discovering that there is a bug is one thing, discovering why is
another. The problems that result from silently doing the wrong thing
can be significantly harder to diagnose than an exception traceback
is, and this hasattr_lite would let me get an exception in cases where
I might otherwise have silently wrong behavior.

I mean, yes, this error was in fact found in my unit test suite. I
spent a lot of time tracking it down (perhaps too much time, because I
was expecting something else [oops, too many changes in one changeset
:X]), and eventually narrowed it down to a try/except
AttributeError I had. Even then, trying to filter out the legitimate
AttributeErrors and the illegitimate one from the same test case was
annoying. I ended up breaking at the except block and individually
examining the exceptions, both legitimate and not. This solved
everything.

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.

-- Devin



More information about the Python-ideas mailing list