By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,626 Members | 2,202 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,626 IT Pros & Developers. It's quick & easy.

modifying def behaviour

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Craig Zoretich" <cz*******@octigabay.com> wrote in message
news:e7**************************@posting.google.c om...
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)
myfunc(3)

Called: myfunc with args (3,)
3

Terry J. Reedy
Jul 18 '05 #2

P: n/a
"Terry Reedy" <tj*****@udel.edu> wrote in message news:<UX********************@comcast.com>...
"Craig Zoretich" <cz*******@octigabay.com> wrote in message
news:e7**************************@posting.google.c om...
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)
myfunc(3)

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
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.