where am I ?

Michele Simionato mis6 at pitt.edu
Wed Mar 12 16:00:44 EST 2003


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 ?

TIA,


                                                    Michele




More information about the Python-list mailing list