Identifying Caller
Alex Martelli
aleaxit at yahoo.com
Sun Oct 12 16:43:46 EDT 2003
Tonguç Yumruk wrote:
> Is there a way to identify the caller of a function? For example:
>
> def foo():
> print <Caller Functions name>
>
> def bar():
> foo()
>
> I want foo to print "bar"... And it will be great if I can also detect
> call is came from which module.
See function _getframe in module sys. The frame object that
it returns has many attributes useful for your tasks. In particular,
sys._getframe(1).f_code.co_name should be 'bar' as you require,
and sys._getframe(1).f_globals['__name__'] should be the module
name which you desire.
Alex
More information about the Python-list
mailing list