On this page
  1. functools.wraps
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

functools.wraps

Prev: functools.update_wrapper | Up: functools
Rel: functools.cache, functools.cached_property, functools.cmp_to_key, functools.lru_cache
Ref: docs.python.org — functools.wraps
Tags: #public #python3 #stdlib #functools


Import

import functools
from functools import wraps

Signature & Overview

functools.wraps(wrapped, assigned=('__module__', '__name__', '__qualname__', '__doc__', '__annotate__', '__type_params__'), updated=('__dict__',))

Decorator factory to apply update_wrapper() to a wrapper function Returns a decorator that invokes update_wrapper() with the decorated function as the wrapper argument and the arguments to wraps() as the

Usage

from functools import wraps

def double_result(fn):
    @wraps(fn)
    def wrapper(*args, **kwargs):
        return fn(*args, **kwargs) * 2
    return wrapper

@double_result
def add(a: int, b: int) -> int:
    """Add two ints."""
    return a + b

assert add(2, 3) == 10
assert add.__name__ == "add" and add.__doc__ == "Add two ints."

Gotchas

REPL Inspect

from functools import wraps
help(wraps)

Linked from

Related notes