[Python-ideas] Standard way to get caller name and easy call stack access

Mark Shannon mark at hotpy.org
Thu Mar 22 10:24:40 CET 2012

anatoly techtonik wrote:
> On Thu, Mar 22, 2012 at 3:17 AM, Sven Marnach <sven at marnach.net> wrote:
>> anatoly techtonik schrieb am Do, 22. Mär 2012, um 00:03:47 +0300:
>>> Seems like the only way to get caller name in Python is to use inspect
>>> module [1]
>>> It seems that this way is not recommended (I wonder why?) and there is no other.
>> Using introspection in production code is not recommended in general,
>> with some exceptions, since the main purpose of introspection is
>> debugging.  *If* you want to do this at all, the recommended way is to
>> use the inspect module.  (And there is another way -- at least in
>> CPython you can use 'sys._getframe().f_back.f_code.co_name'.)
> If sys._getframe() is not available in Jython, IronPython and PyPy
> then I won't use that for debug mode output in libraries.

sys._getframe() is available in PyPy and Jython, I'm not sure if 
IronPython supports it, I think it does.

A simpler and more concrete suggestion might be to simply rename
sys._getframe() as sys.getframe(), since all the main implementations
support it anyway.


More information about the Python-ideas mailing list