[Python-Dev] hasattr and properties (original) (raw)

Thomas Lotze tl at gocept.com
Wed Dec 7 19:40:36 CET 2005


Hi,

I've noticed some behaviour of hasattr when used on properties which I'm inclined to call a bug, or at least unexpected behaviour:

Python 2.4.2 (#1, Oct 29 2005, 13:11:33) [GCC 3.3.6 (Gentoo 3.3.6, ssp-3.3.6-1.0, pie-8.7.8)] on linux2 [...]

class Foo(object): ... def get(self): ... print "hi there" ... raise Exception ... bar = property(get) ... hasattr(Foo, "bar") True hasattr(Foo(), "bar") hi there False

One would expect hasattr to yield the same result in both cases, and the result to be True.

Apparently, when applied to a class instance, hasattr calls getattr and decides that the attribute doesn't exist if the call raises any exception.

-- Thomas



More information about the Python-Dev mailing list