Python interceptor package

Peter Otten __peter__ at web.de
Thu Jun 17 04:56:57 EDT 2004


Fritz Bosch wrote:

> What I'm looking for is an Interceptor class whose
> constructor would interpose its (callable) instance
> between a caller and a specified function or method,
> and whose 'destroy' method would remove the interposition.

> I have actually started to experiment with an Interceptor
> class, whose constructor modifies the __dict__ of a specified
> class, to 'wrap' its  (callable) instance around the specified
> method.

> So, comments are invited!

Maybe there isn't such a package because the following is often sufficient:

def wrap(method):
    def wrapped(self, *args, **kw):
        print "begin"
        method(self, *args, **kw)
        print "end"
    return wrapped


class Test(object):
    def method(self, name):
        print "method(%r)" % name

t = Test()
t.method("pure")
Test.method = wrap(Test.method)
t.method("wrapped")

Peter





More information about the Python-list mailing list