[Python-Dev] hasattr and properties

Guido van Rossum guido at python.org
Thu Dec 8 02:38:54 CET 2005


On 12/7/05, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Maybe descriptors need a fourth slot for hasattr
> customisation?
>
> The logic would then be
>
>    if there is a descriptor for the attribute:
>      if the descriptor's hasattr slot is populated:
>        return the result of calling it
>      else:
>        return True
>    else:
>      look in the instance dict for the attribute

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

IMO a property that has a side effect (other than updating a cache or
statistics or perhaps logging) is a misfeature anyway, so I don't see
what's wrong with hasattr() trying getattr() and reporting False IFF
that raises an exception.

If you want only AttributeError to be handled, use getattr(x, 'name', None).

--
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list