Function calls and reflection
Peter Hansen
peter at engcorp.com
Sun Jan 5 21:53:00 EST 2003
Christian Tismer wrote:
>
> Jeremy Yallop wrote:
> > * Boethius
> > | Can a function tell which function / method called it?
> >
> > Yes.
> >
> > Here's one way:
> >
> > from inspect import currentframe
>
> Note that for simple current frame access, you don't need
> to import inspect.
> Inspect's currentframe, for Python 2.x, is identical
> to sys._getframe.
Python 2.1 and up, actually. As mentioned at http://www.amk.ca/python/2.1/ :
"A new implementation-dependent function, sys._getframe([depth]),
has been added to return a given frame object from the current
call stack. sys._getframe() returns the frame at the top of the
call stack; if the optional integer argument depth is supplied,
the function returns the frame that is depth calls below the top
of the stack. For example, sys._getframe(1) returns the caller's
frame object.
-Peter
More information about the Python-list
mailing list