Return name of caller function?

Stephen R Laniel steve at laniels.org
Wed Jun 27 16:36:16 EDT 2007


On Wed, Jun 27, 2007 at 01:25:14PM -0700, Matthew Peter wrote:
> Parsing the stack's tuple to get those attributes didn't feel reliable or pythonic.
> I am likely overlooking something. Is there a brief example you could show me in the
> context of using inspect to accomplish the goal I outlined above? The goal is using
> a function and not a class. Thanks!

The code below doesn't do the trick for you?

#!/usr/bin/python
import inspect

def master():
    print "I am the master"
    slave()

def slave():
    stack = inspect.stack()
    caller = stack[1][3]
    print "I am the slave; my caller was %s" % caller

def main():
    master()

if __name__ == '__main__':
    main()

-- 
Stephen R. Laniel
steve at laniels.org
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key



More information about the Python-list mailing list