[Python-Dev] hasattr and properties

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Dec 9 04:31:19 CET 2005


Guido van Rossum wrote:

> Um, that does't work for types which customize __getattribute__ or
> __getattr__ in various ways.

There could be a __hasattr__ slot in the class itself for
that purpose.

> IMO a property that has a side effect (other than updating a cache or
> statistics or perhaps logging) is a misfeature anyway,

Even if it doesn't have side effects, getting the attribute's
value could be expensive. One shouldn't have to pay the full
cost of getting the value just to find out whether the attribute
itself exists.

To put it another way, hasattr is enquiring about the object's
interface, not asking it to perform a computation. Doing the
latter when only being asked to do the former is wrong, IMO.

Greg


More information about the Python-Dev mailing list