Wrapping method calls with metaclasses

Alex Martelli aleax at mail.comcast.net
Wed Dec 7 05:06:02 CET 2005

Lawrence Oluyede <raims at dot.com> wrote:

> I've never used metaclasses in real life before and while searching through
> the online Cookbook I found this gorgeous example:
> "Wrapping method calls (meta-class example)"
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/198078
> What I want to do in my app is to log all method calls and seems that the
> metaclass example above do this fine but it fails when it's needed to
> instantiate the class I'm in in a method. So if I do:
> class Test(object):
>     __metaclass__ = LogTheMethods
>     def foo(self):
>       return Test()
> this generate a Runtime Error cause recursion never stops :(
> Any hint to enhance the example?

I can't reproduce the infinite recursion you observe, by merging said
recipe and your definition of class Test, w/Python 2.4.2 on Mac OS 10.4.

Can you please supply a minimal complete example, e.g. simplifying
function logthemethod to just emit s/thing to stdout etc, that does
exhibit the runaway recursion problem you observe?


More information about the Python-list mailing list