Intercept methods/raise exception
Gordon McMillan
gmcm at hypernet.com
Thu Jul 20 19:45:40 EDT 2000
Remco Gerlich wrote:
>Thomas Weholt wrote in comp.lang.python:
>> I tried using __getattr__, like
>>
>> def __getattr__(self, attrname):
>> print 'Trace:', attrname
>> return getattr(self, attrname)
>>
>> But it crashed terribly. The object in question is accessed lots of
>> times. When I ran it, it printed :
>> Trace: __coerce__
>> Some hundred, thousands times, then collapsed with a bang.
>
>This is simple infinite recursion.
You got that right!
> The getattr() call calls __getattr__,
>etc.
>
>Try returning self.__dict__[attrname].
And that wrong! If the object exists, __getattr__ never gets called. You
can trap all assigments to attributes through __setattr__, but only by
using a proxy can you trap all accesses.
- Gordon
More information about the Python-list
mailing list