Given a function object assigned to a name. How to get the name of the function as a string?
For example, consider the following function
your_function. How to get the name
"your_function" from this?
def your_function(): pass string_name = ???
Your desired value of the result stored in
string_name is the string
print(string_name) # your_function
Method 1: Use the __name__ Attribute
The most Pythonic way to extract the name from any object or class is to use its
__name__ attribute. Attributes enclosed in double underscores (dunder attributes) provide special functionality to Python programmers. For example, calling
your_function.__name__ yields the string value
def your_function(): pass string_name = your_function.__name__ print(string_name) # your_function
However, there’s an important case where this method will fail.
💣 ATTENTION: In Python, functions are objects. You can create multiple variables pointing to the same object in memory. Therefore, you could create two names
func_2 that both point to the same function object. When calling
func_1.__name__ the result may be
func_2 instead of
func_1 what you expected.
This is showcased in the following minimal example:
def func_2(): pass func_1 = func_2 print(func_1.__name__) # func_2
As both names point to the same memory object, but
func_2 was pointing to it first, the name of the object is
func_2. The memory usage of this code is visualized using the Python Tutor tool:
Both variables refer to the same function object that defaults as
Method 2: Use the __qualname__ attribute
To extract the name from any object or class, you can also use its
__qualname__ attribute. For example, calling
your_function.__qualname__ yields the string value
'your_function'. You’d use
__name__ if you’d also need some context such as the class defining the method.
The following code creates a function and a method inside a class. It then compares the output of the
__qualname__ attributes for both the function and the class:
def my_function(): pass class My_Class(object): def my_method(self): pass print(my_function.__name__) # "my_function" print(My_Class.my_method.__name__) # "my_method" print(my_function.__qualname__) # "my_function" print(My_Class.my_method.__qualname__) # "My_Class.my_method"
Thanks for reading this article. To boost your Python skills, consider joining the free Finxter email academy with plenty of cheat sheets, Python tutorials, and fun programming puzzles! 🙂
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.