How to get a function's name in Python

To get a function’s name in Python, you need to access the __name__ attribute from the function object.

Here’s an example of how to get a function’s name:

def greet():
    print("Hello there!")

x = greet

print(x.__name__)

Output:

greet

The __name__ is a shared Python special attribute that returns the name of the object as a string.

This attribute is great because it also works for getting a module or a class name.

I hope this tutorial helps. Happy coding! 👍

More Python tutorials

See all