Intercept methods/raise exception

Thomas Weholt thomas at cintra.no
Thu Jul 20 09:06:13 EDT 2000


On Thu, 20 Jul 2000 12:41:16 GMT, thomas at cintra.no (Thomas Weholt)
wrote:

>Hi,
>
>Is there a way to define a method in a class that is called each time
>every other method is called in that class, and based on a attribute,
>raise an exception or let the method-class go thru?
>
>If I could raise some exception each time an object was accessed no
>matter what, that would be great too, but I guess that`s stretching it
>a bit far.
>
>I need this to control a huge object that's manipulated in many ways,
>not always by it's own methods, sometime by altering attributes
>directly. If I could trap any change in an objects properties or use
>of its methods, that would be the best thing.
>
>Thanks.
>
>Thomas

Hi, me again.

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.

Any ideas??

Thomas



More information about the Python-list mailing list