473,399 Members | 3,656 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,399 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 1150
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.