Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get function name as a string in python [duplicate]

Possible Duplicate:
How do I get the name of a function or method from within a Python function or method?
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'.

like image 435
rectangletangle Avatar asked Nov 30 '22 16:11

rectangletangle


2 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
like image 199
Umang Avatar answered Dec 06 '22 02:12

Umang


Use __name__.

Example:

def foobar():
    pass

bar = foobar

print foobar.__name__   # prints foobar
print bar.__name__   # still prints foobar

For an overview about introspection with python have a look at http://docs.python.org/library/inspect.html

like image 41
Ulrich Dangel Avatar answered Dec 06 '22 02:12

Ulrich Dangel