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

Michael Foord fuzzyman at voidspace.org.uk
Mon Aug 23 22:08:50 CEST 2010


  On 23/08/2010 22:59, Benjamin Peterson wrote:
> [snip...]
>> IMO, this is a much better solution, more in line with known use cases
>> for hasattr().   If the proposed change when through, it would fail to
>> address the common use case and cause people to start writing their
>> own versions of hasattr() that just scan but do not run code.
> Can you provide an example? I've never seen code which explicitly
> scans MRO and dicts to avoid triggering code. (Besides
> collections.Callable; that's a special case.)
>

The example I linked to in my previous email did exactly that - the use 
case was for finding and displaying docstrings on members in an 
interactive object viewer. We needed to be able to examine objects 
without triggering code execution in them.

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.

Michael


>


-- 
http://www.ironpythoninaction.com/



More information about the Python-Dev mailing list