function name

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Apr 29 01:38:54 CEST 2010


On Wed, 28 Apr 2010 21:03:36 +0200, Peter Otten wrote:

> Richard Lamboj wrote:
> 
>> is there any way to get the name from the actual called function, so
>> that the function knows its own name?
> 
>>>> import sys
>>>> def my_name():
> ...     return sys._getframe(1).f_code.co_name 

Be aware though, that sys._getframe is marked as a private function (the 
leading underscore), which means that:

(1) It only exists in some Python implementations (CPython and possible 
nothing else?); and

(2) It is officially subject to change without warning, although that's 
unlikely.


In practice, it's probably safe to use, but be aware that you are using a 
documented internal feature.


-- 
Steven



More information about the Python-list mailing list