[Python-Dev] 'hasattr' is broken by design

Guido van Rossum guido at python.org
Tue Aug 24 16:50:58 CEST 2010


On Tue, Aug 24, 2010 at 4:51 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> But that's the thing... as far as I am concerned, a dynamically defined
> attribute *doesn't* exist. If it existed, __getattr__ would never be
> called. A minor semantic difference, to be sure, but it's real to me.

Eh? If "x.y" succeeds, in what sense does y not exist?

> Whether I should care about the difference is a separate issue.

Right, you are breaking through too much abstraction.

> Performance could be an issue, of course, if somebody writes an
> expensive __getattr__ or property. Computed attributes should be cheap.

Yes, and it should be considered the problem of the author of that
property, not of the user.

> But I'm actually more concerned about side-effects than performance.

Properties should not have side effects. That would be a problem when
using them just as much as with hasattr().

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list