What function called this function ??
Mark McEahern
marklists at mceahern.com
Wed Sep 25 11:04:49 EDT 2002
> Is it possible, by hacking the traceback/stack/etc, to get
> information about the function, that functions class and in what
> module that function and class are defined, from inside a function
> located elsewhere? Something like a way of answering the question
> of "who called me?" without sending the answer along as a parameter.
Some variation on this theme:
**
#!/usr/bin/env python
def mycaller():
"""mycaller()
Return the name of the caller of the caller of this function.
"""
import sys
steps_back = 2
return sys._getframe(steps_back).f_code.co_name
def foo():
print "foo called by %s." % mycaller()
def bar():
foo()
bar()
**
Output:
foo called by bar.
// m
More information about the Python-list
mailing list