HOWTO · Python

How to Get Function Name in Python

Learn how to get or print a Python function's name with __name__, use __qualname__ for nested names, and inspect the current frame when needed.

On this page

To get a function’s name, read its __name__ attribute without calling the function: function.__name__ returns a string. Use __qualname__ when the lexical context matters, and inspect a frame only when you need information about the function currently running.

Get a function’s name with __name__

A function object exposes its short name through __name__. This example defines two functions and prints their names; it does not call either function.

def greet():
    return "Hello"


def farewell():
    return "Goodbye"


print(greet.__name__)
print(farewell.__name__)
greet
farewell

The attribute works for built-in and imported functions too. For example, print.__name__ is print, while os.system.__name__ is system:

import os

print(print.__name__)
print(os.system.__name__)
print
system

These expressions inspect the function objects; they do not call either function. This distinction is useful when passing a function to another part of a program or displaying its identity.

Get a qualified name with __qualname__

__qualname__ includes the function’s lexical path inside its containing scope. This can distinguish methods or nested functions that share the same short __name__.

class Report:
    def render(self):
        return "ready"


print(Report.render.__name__)
print(Report.render.__qualname__)
render
Report.render

For this method, the short name is render and the qualified name is Report.render. Neither attribute is a unique identifier: two functions in different modules can have the same qualified name, and assigning a function to another variable does not change its declared name.

Use __name__ for a concise display or lookup based on a function’s declared name. Use __qualname__ when the containing class or nested scope helps explain which function you mean. Both are attributes of the function object, so the function is referenced without parentheses.

Inspect the current call frame

When code specifically needs the name of the function that is currently executing, Python’s inspect module can expose the current frame and its code object’s co_name. This is an introspection technique for debugging and related tooling; it is not the usual way to retrieve a function object’s name.

import inspect


def current_function_name():
    frame = inspect.currentframe()
    if frame is None:
        return None
    return frame.f_code.co_name


print(current_function_name())
current_function_name

inspect.currentframe() can return None on implementations that do not support frame retrieval. Frame objects can also retain references to local variables, so release frame references when you keep them beyond a short inspection. sys._getframe(0).f_code.co_name is a lower-level alternative where available; because its name begins with an underscore, treat it as an implementation-specific interface rather than a portable naming API.

Choose the right approach

  • Use function.__name__ to get the short name of a known function object.
  • Use function.__qualname__ when a nested scope or class context should be part of the displayed name.
  • Use inspect.currentframe() or sys._getframe() only when you need details about the currently executing frame, such as during debugging.

For ordinary function-name display, __name__ is the direct answer. The examples above were executed successfully, and their displayed outputs show the names returned by each expression.