faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

4 of 4 people (100%) answered Yes
Recently 2 of 2 people (100%) answered Yes

Entry

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

Jul 5th, 2000 10:02
Nathan Wallace, Hans Nowak, Snippet 231, Adrian Eyre


"""
Packages: miscellaneous.introspection
"""
"""
> 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.
"""
import sys
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()