"What is the name of the function/method that called me?"

Adrian Eyre a.eyre at optichrome.com
Fri Oct 15 11:07:24 EDT 1999


> Given Python's run-time typing and the ability to generate call stacks
> of line numbers and module names, I suspect there is a way to answer
> this question, but it has escaped my perusal of my books and the online
> documentation.  Help appreciated.

def foo():
    bar()
	
def bar():
    print "I was called by " + who_called_me()
	
def who_called_me():
    try:
        raise "Hack"
    except:
        return sys.exc_info()[2].tb_frame.f_back.f_back.f_code.co_name

if __name__ == '__main__':
    foo()

--------------------------------------------
Adrian Eyre <mailto:a.eyre at optichrome.com>
Optichrome Computer Solutions Ltd
Maybury Road, Woking, Surrey, GU21 5HX, UK
Tel: +44 1483 740 233  Fax: +44 1483 760 644
http://www.optichrome.com 
--------------------------------------------





More information about the Python-list mailing list