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

Yury Selivanov yselivanov at gmail.com
Mon Aug 23 23:22:55 CEST 2010


On 2010-08-23, at 5:02 PM, Michael Foord wrote:

> On 23/08/2010 23:55, Benjamin Peterson wrote:
>> 2010/8/23 Raymond Hettinger<raymond.hettinger at gmail.com>:
>>> On Aug 23, 2010, at 1:13 PM, Benjamin Peterson wrote:
>>> 
>>>> 2010/8/23 Michael Foord<fuzzyman at voidspace.org.uk>:
>>>>> To me hasattr *looks* like a passive introspection function, and the fact
>>>>> that it can trigger arbitrary code execution is unfortunate - especially
>>>>> because a full workaround is pretty arcane.
>> hasattr(x, "y") doesn't look any more passive to me the x.y.
> One says "does this object have attribute y" the other fetches attribute y. I'm amazed they don't look different to you. Given Python's object model there is no reason that the first *should* fetch the attribute to determine that it is present, *except* for the dynamic attribute creation of __getattr__ and __getattribute__.

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.

-
Yury



More information about the Python-Dev mailing list