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

Benjamin Peterson benjamin at python.org
Mon Aug 23 23:12:50 CEST 2010


2010/8/23 Michael Foord <fuzzyman at voidspace.org.uk>:
> Properties are allowed to do whatever the heck they want. That doesn't mean
> that you have to execute code to determine whether they exist or not.

I thought you were trying to determine whether the attribute exists
not the property.
>
> If fetching an attribute raises an AttributeError it doesn't mean that
> attribute doesn't exist (although I admit that at the moment this is exactly
> what hasattr uses to mean) it just means that fetching that attribute raised
> an AttributeError. Even if you allow other exceptions to propagate you are
> still left with the fact that an AttributeError raised as a bug will still
> be silenced and interpreted as meaning that hasattr should just return
> False.

Raised as a bug? Is this not a valid pattern?

@property
def myprop(self):
    if not self.myprop_support:
        raise AttributeError("myprop")



-- 
Regards,
Benjamin


More information about the Python-Dev mailing list