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

Steven D'Aprano steve at pearwood.info
Tue Mar 27 15:33:51 CEST 2012

anatoly techtonik wrote:
> Well, I proposed to have a caller_name() method. If it is not too
> universal, then a sys.call_stack() could be a better try:
>   sys.call_stack() - Get current call stack. Return the list with a
>                             list of qualified names starting with the oldest.
> Rationale: make logging calls and module based filters zero cost.

I don't have an opinion on whether this is a good idea or a bad idea, but if 
people are going to make radical changes with respect to inspecting the call 
stack, I think it would be much better to get a list of the actual calling 
functions, not their names.


* If you have the function, you can do anything with it, including getting the 
name, the docstring, any annotations it may have, etc., but if all you have is 
the name, it isn't so easy, and may not actually be possible to unambiguously 
find the function object given only it's name.

* Not all functions have meaningful names (think lambdas); or their name may 
be ambiguous (think functions produced by factory functions).


More information about the Python-ideas mailing list