[Python-Dev] 'hasattr' is broken by design
P.J. Eby
pje at telecommunity.com
Wed Aug 25 18:27:38 CEST 2010
At 12:10 PM 8/25/2010 +1200, Greg Ewing wrote:
>Consider an object that is trying to be a transparent
>proxy for another object, and behave as much as possible
>as though it really were the other object. Should an
>attribute statically defined on the proxied object be
>considered dynamically defined on the proxy? If so, then
>the proxy isn't as transparent as some people may want.
Yep. That's why the proposed addition to inspect is a bad idea. If
we encourage that sort of static thinking, it will lead to people
creating all sorts of breakage with respect to more dynamic code.
AFAICT, the whole "avoid running code" thing only makes sense for a
debugging tool -- at which point, you can always use the trace
facility and throw an error when any Python code runs that's not part
of your debugging tool. Something like:
def exists(ob, attr):
__running__ = True
# ... set trace function here
try:
try:
getattr(ob, attr)
return True
except AttributeError:
return False
except CodeRanError:
return True # or False if you prefer
finally:
__running__ = False
# restore old tracing here
Where the trace function is just something that throws CodeRanError
if it detects a "call" event and the __running__ flag is True. This
would stop any Python code from actually executing. (It'd need to
keep the same trace function for c_call events, since that might lead
to nested non-C calls .)
Of course, a debugger's object inspection tool would probably
actually want to return either the attribute value, or a special
value to mean "dyanmic calculation needed".
More information about the Python-Dev
mailing list