473,233 Members | 1,602 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,233 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 1147
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.