473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

modifying def behaviour

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
2 1323

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Paul Paterson | last post by:
I am trying to find a way to mimic by-reference argument passing for immutables in Python. I need to do this because I am writing an automated VB to Python converter. Here's an example of the VB...
2
by: Jean-S?bastien Bolduc | last post by:
I'm afraid this is a silly question, to which I know the answer already. But let me ask anyway... In Python, is there a way to modify a builtin type's methods? for instance, modifying...
6
by: qwweeeit | last post by:
Hi all, when I was young I programmed in an interpreted language that allowed to modify itself. Also Python can (writing and running a module, in-line): fNew =open("newModule.py",'w') lNew=...
4
by: Eric | last post by:
I have read that using const_cast to modify an object that was originally declared const can lead to undefined behavior. Would this be true in the case of a user defined object containing a const...
7
by: Jan Gregor | last post by:
Hello folks I want to apply changes in my source code without stopping jython and JVM. Preferable are modifications directly to instances of classes. My application is a desktop app using swing...
7
by: Andy Lomax | last post by:
The C99 standard contains various statements like this one (in this case, 6.5.16, assignment operator): >If an attempt is made to modify >the result of an assignment operator or to access it...
7
by: Bill | last post by:
Hello, Here is some code: I'm thinking that when storing an object in an ArrayList or Hashtable that it clones the object. This can be shown like so: // key is a generated hashcode for this...
7
by: Ivan Voras | last post by:
For a declaration like: List<MyTypeitems = ... where MyType is a struct, attempt to modify an item with items.member = something; fails with the message:
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
5
by: IUnknown | last post by:
Ok, we are all aware of the situation where modifying the folder structure (adding files, folders, deleting files, etc) will result in ASP.NET triggering a recompilation/restart of the application....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.