Functions, callable objects, and bound/unbound methods

Kent Johnson kent at kentsjohnson.com
Fri Dec 1 12:26:46 EST 2006


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.

Kent



More information about the Python-list mailing list