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! 👍

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.