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

P.J. Eby pje at telecommunity.com
Tue Aug 24 00:45:32 CEST 2010


At 06:12 PM 8/23/2010 -0400, Yury Selivanov wrote:
>BTW, is it possible to add new magic method __hasattr__?  Maybe not
>in Python 3.2, but in general.

In order to do this properly, you'd need to also add __has__ or 
__exists__ (or some such) to the descriptor protocol; otherwise you 
break descriptors' ability to operate independently of the class 
they're used in.  You would probably also need a __hasattribute__, in 
order to be able to properly synchronize with __getattr__/__getattribute__.

Seems like overkill to me, though, as I'm not sure how such a 
protocol actually helps ORM or persistence schemes (and I've written 
a few).  Pretty much, if you're trying to check for the existence of 
an attribute, you're probably about to be getting that attribute 
anyway.  (i.e. why query the existence of an attribute you *don't* 
intend to use?)



More information about the Python-Dev mailing list