3

Trying to get a function name in Python, I would like to achieve something like this:

def my_function():
    do_something...

def get_func_name(function):
    return magic(function)

>>> print get_func_name(my_function)
>>> my_function

How should this be done?

Thanks a lot!

0

2 Answers 2

4

Python has a magical attribute __name__.

def get_func_name(function):
    return function.__name__
Sign up to request clarification or add additional context in comments.

Comments

3
def my_function():
    pass

def get_func_name(function):
    try:
        # Python2
        return function.func_name
    except AttributeError:
        # Python3
        return function.__name__

print(get_func_name(my_function))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.