modifying def behaviour

C

Craig Zoretich

Hi,

Is it possible to change how the "def" builtin command works?
Specifically I want to modify def to write to a log file when a
function executes (for instance the name of the function, when it was
executed - that sort of thing). I want to avoid having to put some
code in each and every function I write to do this logging for me.

Thanks,
Craig
 
T

Terry Reedy

Craig Zoretich said:
Is it possible to change how the "def" builtin command works?

Sure, if you are willing to change the interpreter. But no, there is
not an exposed 'metafunction' facility analogous to metaclasses.
Specifically I want to modify def to write to a log file when a
function executes (for instance the name of the function, when it was
executed - that sort of thing). I want to avoid having to put some
code in each and every function I write to do this logging for me.

However, the def code has nothing to do with what happens when a
function is called. All it could do is what you don't want (put code
in eash function). So you would need to hook into the function call
mechanism. To do this in Python (rather than in C and recompile the
interpreter) wrap the functions you want logged as instances of a
logger class. Others have previously posted examples like this:

class logger:
def __init__(self, func):
self.func = func
def __call__(self, *args):
print "Called: %s with args %s" % (self.func.func_name, args)
return self.func(*args)

def myfunc(a): return a

myfunc = logger(myfunc)
Called: myfunc with args (3,)
3

Terry J. Reedy
 
C

Craig Zoretich

Terry Reedy said:
Sure, if you are willing to change the interpreter. But no, there is
not an exposed 'metafunction' facility analogous to metaclasses.


However, the def code has nothing to do with what happens when a
function is called. All it could do is what you don't want (put code
in eash function). So you would need to hook into the function call
mechanism. To do this in Python (rather than in C and recompile the
interpreter) wrap the functions you want logged as instances of a
logger class. Others have previously posted examples like this:

class logger:
def __init__(self, func):
self.func = func
def __call__(self, *args):
print "Called: %s with args %s" % (self.func.func_name, args)
return self.func(*args)

def myfunc(a): return a

myfunc = logger(myfunc)

Called: myfunc with args (3,)
3

Terry J. Reedy


Thanks, this was great stuff.

Another question somewhat (maybe) related. Are there going to be any
performance issues related to doing this, speed and memory wise? I am
assuming this will double the amount of objects that will be created
(another object for each function), but being new to python, I don't
know if functions take up a lot of memory or not, and whether I'll
notice the memory difference.

Thanks,
Craig
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top