Accessing stack (context) frames
Fernando PĂ©rez
fperez528 at yahoo.com
Thu Jul 11 22:24:32 EDT 2002
Ingo Blank wrote:
> Hi,
>
> I wonder how it is possible, to access the current context (stack) frame
> in Python.
> In Smalltalk, there is (mostly) a "thisContext" method, from which one
> can retrieve informations like the <name> of the currently executed method.
>
> And that's exactly what I need. Retrieving the <name> of the method,
> currently
> executed.
>
> def someFunction():
> myName = ... # How ?
you want sys._getframe():
In [115]: sys._getframe ?
Type: builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form: <built-in function _getframe>
Namespace: Currently not defined in user session.
Docstring:
_getframe([depth]) -> frameobject
Return a frame object from the call stack. If optional integer depth is
given, return the frame object that many calls below the top of the stack.
If that is deeper than the call stack, ValueError is raised. The default
for depth is zero, returning the frame at the top of the call stack.
This function should be used for internal and specialized
purposes only.
Disregard that warning, it's extremely useful if you know what you're doing :)
f.
More information about the Python-list
mailing list