where am I ?
jeremy at zope.com
Wed Mar 12 22:19:54 CET 2003
On Wed, 2003-03-12 at 16:00, Michele Simionato wrote:
> I would like to define a function 'whereami' that returns information
> about the scope where it is called. For instance if I call 'whereami'
> inside a function f, it should give me f, if I call 'whereami' inside a class
> C, it should give me the class C, etc.
> whereami() #=> __main__
> def f():
> whereami() #=> f
> class C: #=> C
> My guess is that I need to raise an exception and follow the traceback,
> but I am not familiar with this kind of tricks. Any help ?
whereami = lambda: sys._getframe(1).f_code.co_name
The doc string for sys._getframe says:
"""_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
If that is deeper than the call stack, ValueError is raised. The
for depth is zero, returning the frame at the top of the call stack.
This function should be used for internal and specialized
Note that it says internal and specialized purposes, not internal or
specialized purposes :-).
More information about the Python-list