Functions, callable objects, and bound/unbound methods

Ron Garret rNOSPAMon at flownet.com
Fri Dec 1 12:39:22 EST 2006


In article <457065D6.7060406 at kentsjohnson.com>,
 Kent Johnson <kent at kentsjohnson.com> wrote:

> Ron Garret wrote:
> > The reason I want to do this is that I want to implement a trace 
> > facility that traces only specific class methods.  I want to say:
> > 
> > trace(c1.m1)
> > 
> > and have c1.m1 be replaced with a wrapper that prints debugging info 
> > before actually calling the old value of m1.  The reason I want that to 
> > be an instance of a callable class instead of a function is that I need 
> > a place to store the old value of the method so I can restore it, and I 
> > don't want to start building a global data structure because that gets 
> > horribly ugly, and a callable class is the Right Thing -- if there's a 
> > way to actually make it work.
> 
> If the only reason for a callable class is to save a single value (the 
> original function), you could instead store it as an attribute of the 
> wrapper function.

I considered that, and I may yet fall back on it, but 1) I wanted to 
understand how these things worked and 2) I need a way to tell when a 
method has been traced, and isinstance(method, tracer) seems less 
hackish to me than hasattr(method, 'saved_function').

rg



More information about the Python-list mailing list