Get function name as a string in python

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

Possible Duplicate:
http://stackoverflow.com/questions/245304/how-do-i-get-the-name-of-a-function-or-method-from-within-a-python-function-or-m http://stackoverflow.com/questions/245304/how-do-i-get-the-name-of-a-function-or-method-from-within-a-python-function-or-m http://stackoverflow.com/questions/251464/how-to-get-the-function-name-as-string-in-python http://stackoverflow.com/questions/251464/how-to-get-the-function-name-as-string-in-python


I have a function named func , I d like to be able to get the functions name as a string.

pseudo-python :

def func () :
    pass

print name(func)

This would print func .

Answers

That s simple.

print func.__name__

EDIT: But you must be careful:

>>> def func():
...     pass
... 
>>> new_func = func
>>> print func.__name__
func
>>> print new_func.__name__
func

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/7142062/get-function-name-as-a-string-in-python

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils