[Tutor] How to have the name of a function inside the code of this function?
Modulok
modulok at gmail.com
Fri Apr 6 14:22:19 CEST 2012
On 4/6/12, Karim <kliateni at gmail.com> wrote:
>
> Hello all,
>
>
> I have :
>
> def foo():
> print( getattr(foo, 'func_name'))
>
> Where I get the name of the function but I add to give the name of this
> function. Indeed it is not very helpful...
> I checked the globals() but how I can do to get
> globals()['toto'].func_name. This is not more helpful ;o)
>
> If you have any idea to get the caller name inside the caller.
>
The following works, but only on implementations which provide stack frame
support. As the docs kindly point out:
"...this isn't guaranteed to exist in all implementations of Python."
Example code:
import inspect
def foo():
'''Print my own name.'''
frame_info = inspect.getframeinfo(inspect.currentframe())
print(frame_info.function)
foo()
That said, there's probably a better way to solve whatever bigger problem
you're trying solve.
-Modulok-
More information about the Tutor
mailing list