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

Guido van Rossum guido at python.org
Tue Aug 24 00:00:41 CEST 2010


On Mon, Aug 23, 2010 at 2:22 PM, Yury Selivanov <yselivanov at gmail.com> wrote:
> As I understand the only possible way to make 'hasattr' work as it name indicates (i.e. just check if attribute exists, not run it), is to add another magic method(s?) to the existing __getattr__ and __getattribute__ which will tell whether attribute exists or not, and by default this method would mimic current 'hasattr' behaviour.

You nailed it. At the lowest level (either in C or in Python) there is
no way to check for an attribute's presence without getting its value.
This has so far been fundamental, and everything else is just
appearances.

I propose that this is good enough and that we should at this point
not try to invent another protocol, just decide whether hasattr() can
be fixed (and leaving PyObject_HasAttr*() alone).

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list