Access to the Original Function in Decorators

Writing Functions Mar 16, 2026

Access to the Original Function in Decorators – Python 2026 Best Practices

When you apply a decorator, the original function is replaced by the wrapper. However, you can still access the original function using the __wrapped__ attribute. This is very useful for introspection, testing, and advanced decorator patterns.

TL;DR — Key Points 2026

  • Every decorated function has a __wrapped__ attribute pointing to the original function
  • This is automatically added when you use @wraps
  • Useful for calling the original function directly, testing, and debugging
  • Works with stacked (multiple) decorators too

1. Basic Access to Original Function


from functools import wraps
import time

def timer(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        start = time.perf_counter()
        result = func(*args, **kwargs)
        elapsed = time.perf_counter() - start
        print(f"⏱️  {func.__name__} took {elapsed:.6f} seconds")
        return result
    return wrapper

@timer
def calculate(n: int):
    """Heavy calculation function."""
    return sum(i ** 2 for i in range(n))

# Accessing the original function
original_calculate = calculate.__wrapped__

print(calculate.__name__)           # "calculate"
print(original_calculate.__name__)  # "calculate"
print(calculate.__wrapped__ is original_calculate)  # True

# You can call the original directly (without timing)
result = original_calculate(10000)

2. With Multiple (Stacked) Decorators


@timer
@log_call
def process_data(data):
    return [x * 2 for x in data]

# Access the original function (bypassing all decorators)
original = process_data.__wrapped__.__wrapped__   # Go through the stack

# Or use a loop to unwrap all decorators
def unwrap(func):
    while hasattr(func, '__wrapped__'):
        func = func.__wrapped__
    return func

original_process = unwrap(process_data)

3. Best Practices in 2026

  • Use __wrapped__ when you need to bypass decorators (e.g., in tests)
  • Always apply @wraps so __wrapped__ is available
  • For deep decorator stacks, create a helper function to unwrap
  • Be careful not to call __wrapped__ in production code unless necessary
  • Document when you intentionally access the original function

Conclusion

Access to the original function via __wrapped__ is an important feature when working with decorators. In 2026, professional Python developers know how to use this attribute for testing, debugging, and advanced decorator patterns while keeping their code clean and maintainable.

Next steps:

Share:
Last updated: March 2026
Built-in Functions in Python 2026 str() in Python 2026: String Conversion + Modern Formatting & Best Practices memoryview with JAX in Python 2026: Zero-Copy NumPy → JAX Array Interop + Efficient ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + GPU Pinning & ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + ML Examples memoryview with NumPy & PyTorch in Python 2026: Zero-Copy Views, Efficient Slicing & ML Interop Examples memoryview with NumPy in Python 2026: Zero-Copy Views, Efficient Slicing & Real ML Examples memoryview() in Python 2026: Zero-Copy Magic for Large Binary Data + Real Examples import() in Python 2026: How It Works, Security Risks & Modern Alternatives (importlib) zip() in Python 2026: How to Use It, strict=True Behavior & Real-World Examples vars() in Python 2026: Accessing Object Namespace + Modern Introspection Patterns type() in Python 2026: Dynamic Type Inspection & Object Creation + Modern Patterns tuple() in Python 2026: Immutable Sequences + Modern Patterns & Best Practices super() in Python 2026: Method Resolution & Modern Inheritance Patterns sum() in Python 2026: Summing Iterables + Modern Numeric Patterns & Best Practices staticmethod() in Python 2026: Static Methods, Utility Functions & Modern Best Practices sorted() in Python 2026: Sorting Iterables + Modern Patterns & Best Practices slice() in Python 2026: Creating Slice Objects + Modern Patterns & Best Practices setattr() in Python 2026: Dynamic Attribute Setting + Modern Patterns & Safety set() in Python 2026: Mutable Sets Creation + Modern Patterns & Best Practices