where am I ?
Jeremy Hylton
jeremy at zope.com
Wed Mar 12 16:19:54 EST 2003
More information about the Python-list mailing list
Wed Mar 12 16:19:54 EST 2003
- Previous message (by thread): where am I ?
- Next message (by thread): where am I ?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 > whereami() > > 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 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. """ Note that it says internal and specialized purposes, not internal or specialized purposes :-). Jeremy
- Previous message (by thread): where am I ?
- Next message (by thread): where am I ?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list