Python
Programming
Function Naming
Introspection
Code Debugging

Determine function name from within that function

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Python, the cleanest way to know a function's own name from inside that function is usually __name__. For more dynamic introspection, you can also inspect the current frame, but that is heavier and usually less desirable than just using the function object's built-in metadata.

Use __name__ in Normal Functions

Inside a function body, the function object can expose its declared name through __name__.

python
1def greet():
2    print(greet.__name__)
3
4
5greet()

This is simple, readable, and usually enough for logging or debugging.

Use inspect When You Need the Current Frame Name

If you specifically want the current executing frame's function name, use the inspect module.

python
1import inspect
2
3
4def greet():
5    current_name = inspect.currentframe().f_code.co_name
6    print(current_name)
7
8
9greet()

This works even when you do not want to reference the function object by its declared symbol directly.

Know Why inspect Is Heavier

Frame inspection is more dynamic and more expensive than reading __name__. It is useful for debugging and generic logging helpers, but it should not be your first choice in hot paths if a simpler answer exists.

That is why most ordinary code should prefer the direct function.__name__ route.

Decorators Can Change What You See

If a function is wrapped by a decorator, the visible function metadata may come from the wrapper unless the decorator preserves metadata with functools.wraps.

python
1from functools import wraps
2
3
4def logged(func):
5    @wraps(func)
6    def wrapper(*args, **kwargs):
7        print(func.__name__)
8        return func(*args, **kwargs)
9    return wrapper
10
11
12@logged
13def greet():
14    pass

Without @wraps, names and docstrings can become misleading.

Logging Helpers Often Want the Caller Name

Sometimes the real goal is not "what is my own function name?" but "what function called this helper?" That is a different question and usually requires stack or frame inspection. Be precise about which one you actually need before choosing an approach.

That distinction saves a lot of confusion when building debug utilities.

Prefer Explicit Logging When Possible

It is tempting to rely on introspection everywhere, but explicit log messages are often clearer and less fragile. Introspection is useful, especially in shared helpers, but it should support clarity rather than replace it.

__qualname__ Can Be Useful Too

If nested functions or methods matter, __qualname__ can provide more context than __name__ by including the qualified path of the function within its enclosing scope.

Methods and Bound Functions Follow the Same Idea

For instance methods, the method object still exposes name metadata, and introspection still works. The main difference is usually whether you care about the plain function name or a more qualified dotted path for debugging.

Avoid inspect Unless the Extra Flexibility Helps

The inspect module is powerful, but most code does not need full frame introspection just to print a name. Reaching for the simpler metadata first usually keeps the implementation cleaner.

Common Pitfalls

  • Using frame inspection when __name__ would be enough.
  • Forgetting that decorators can hide or replace the original function metadata.
  • Confusing the current function name with the caller's function name.
  • Treating introspection as free in performance-sensitive code.
  • Relying on dynamic stack inspection when a direct explicit label would be simpler.

Summary

  • In normal Python code, function.__name__ is the simplest way to get a function's name.
  • 'inspect.currentframe().f_code.co_name gives the executing frame's name when you need introspection.'
  • Decorators can affect what function metadata you see.
  • Caller-name lookup is a different problem from self-name lookup.
  • Prefer the simplest approach that matches the logging or debugging goal.

Course illustration
Course illustration

All Rights Reserved.