471,334 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

function call

Hey all,

Is there a way of printing out how a function was called? In other
words if I do the following:

def someFunction(self):
self.someOtherFunction(var1, var2)
I would get something like "someOtherFunction: called by:
someFunction, args are: var1, var2"

Thanks in advance

- ianaré

Sep 4 '07 #1
7 1110
ianaré a écrit :
Hey all,

Is there a way of printing out how a function was called? In other
words if I do the following:

def someFunction(self):
self.someOtherFunction(var1, var2)
I would get something like "someOtherFunction: called by:
someFunction, args are: var1, var2"

Thanks in advance
You may be able to solve this using a decorator (to avoid polluting your
code with this) and the infamous sys._getframe() hack.
Sep 4 '07 #2
On Sep 4, 3:17 pm, ianaré <ian...@gmail.comwrote:
Hey all,

Is there a way of printing out how a function was called? In other
words if I do the following:

def someFunction(self):
self.someOtherFunction(var1, var2)

I would get something like "someOtherFunction: called by:
someFunction, args are: var1, var2"

Thanks in advance

- ianaré
I think you can use __name__, but I'm not sure how to apply it here.
Read up on Python's introspection tools:

http://www.ibm.com/developerworks/library/l-pyint.html

Mike

Sep 4 '07 #3
Bruno Desthuilliers a écrit :
ianaré a écrit :
>Hey all,

Is there a way of printing out how a function was called? In other
words if I do the following:

def someFunction(self):
self.someOtherFunction(var1, var2)
I would get something like "someOtherFunction: called by:
someFunction, args are: var1, var2"

Thanks in advance
You may be able to solve this using a decorator (to avoid polluting your
code with this) and the infamous sys._getframe() hack.
Not even with sys._getframe in fact - or at least not directly !-)
import inspect

def trace(func):
def traced(*args, **kw):
f = inspect.currentframe(1)
caller = inspect.getframeinfo(f)[2]
print "%s : called by %s with %s %s" \
% (caller, func.__name__, str(args), kw)
return func(*args, **kw)
return traced

@trace
def test(toto, tata=None):
return 42

def call(toto):
return test(toto, 24)

call("foo")

HTH
Sep 4 '07 #4
On 9/3/07, Bruno Desthuilliers <bd*****************@free.quelquepart.frwrote:
ianaré a écrit :
Hey all,

Is there a way of printing out how a function was called? In other
words if I do the following:

def someFunction(self):
self.someOtherFunction(var1, var2)
I would get something like "someOtherFunction: called by:
someFunction, args are: var1, var2"

Thanks in advance
You may be able to solve this using a decorator (to avoid polluting your
code with this) and the infamous sys._getframe() hack.
--
Every reasonable use case for this (and several unreasonable ones)
that I've encountered (and some that I've just imagined) can be better
addressed with a trace function (sys.set_trace) than by trying to do
this at the point of call.
Sep 4 '07 #5
Chris Mellon a écrit :
On 9/3/07, Bruno Desthuilliers <bd*****************@free.quelquepart.frwrote:
>>ianaré a écrit :
>>>Hey all,

Is there a way of printing out how a function was called? In other
words if I do the following:

def someFunction(self):
self.someOtherFunction(var1, var2)
I would get something like "someOtherFunction: called by:
someFunction, args are: var1, var2"

Thanks in advance

You may be able to solve this using a decorator (to avoid polluting your
code with this) and the infamous sys._getframe() hack.
--


Every reasonable use case for this (and several unreasonable ones)
that I've encountered (and some that I've just imagined) can be better
addressed with a trace function (sys.set_trace) than by trying to do
this at the point of call.
Indeed. Thanks for the correction.

<op>
relevant doc here:
http://docs.python.org/lib/debugger-hooks.html
</op>
Sep 4 '07 #6
On Tue, 04 Sep 2007 13:17:39 -0700, ianaré wrote:
Hey all,

Is there a way of printing out how a function was called? In other words
if I do the following:

def someFunction(self):
self.someOtherFunction(var1, var2)
I would get something like "someOtherFunction: called by: someFunction,
args are: var1, var2"
The obvious way is:

def someFunction(self):
print "someOtherFunction: called by: someFunction," \
"args are: %s, %s" % (var1, var2)
self.someOtherFunction(var1, var2)

but that has obvious inconveniences.

Perhaps you should look at the Python debugger and profiler? Do they
solve the underlying problem you are trying to solve?

--
Steven.
Sep 4 '07 #7
Every reasonable use case for this (and several unreasonable ones)
that I've encountered (and some that I've just imagined) can be better
addressed with a trace function (sys.set_trace) than by trying to do
this at the point of call.

Indeed. Thanks for the correction.

<op>
relevant doc here:http://docs.python.org/lib/debugger-hooks.html
</op>
Thanks for your help. I was hoping to see a quick and easy way to do
this, so this was helpful for this particular situation. I did not
want to set up a debugger, although I am aware it's the "correct"
thing to do ...

Cheers!
Sep 4 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by JKop | last post: by
2 posts views Thread by laredotornado | last post: by
23 posts views Thread by bluejack | last post: by
21 posts views Thread by dragoncoder | last post: by
3 posts views Thread by Beta What | last post: by
9 posts views Thread by CryptiqueGuy | last post: by
9 posts views Thread by Morten Lemvigh | last post: by

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.