function call

  • Thread starter Bruno Desthuilliers
  • Start date
B

Bruno Desthuilliers

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.
 
B

Bruno Desthuilliers

Bruno Desthuilliers a écrit :
ianaré a écrit :

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
 
B

Bruno Desthuilliers

Chris Mellon a écrit :
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>
 
?

=?iso-8859-1?B?aWFuYXLp?=

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é
 
K

kyosohma

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
 
C

Chris Mellon

ianaré a écrit :
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.
 
S

Steven D'Aprano

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?
 
?

=?iso-8859-1?B?aWFuYXLp?=

Every reasonable use case for this (and several unreasonable ones)
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top