Meaning
Copies attributes such as __module__, __name__, __doc__, and __annotations__ from a source function to a wrapper function. This preserves metadata that would otherwise be lost when decorating a function. Used when writing decorators that need the wrapped function to retain its original identity for introspection, documentation, and debugging.
Primary Function
Metadata preservation
Communicative Purpose
Ensures decorated functions retain their original metadata for proper introspection and documentation.
Pattern
functools.update_wrapper(wrapper, func)
Core Structure
functools.update_wrapper(..., ...)
Função primária
Metadata preservation
Propósito comunicativo
Ensures decorated functions retain their original metadata for proper introspection and documentation.
Situações de gatilho
Python decorators: preserving function metadata when wrapping a function with another function; Framework middleware: copying __name__ and __doc__ from original handler to wrapper for logging and debugging
Contextos
Python standard library, web frameworks (Flask, Django), library development, any code using decorators
Padrão
functools.update_wrapper(wrapper, func)
Estrutura central
functools.update_wrapper(..., ...)
Slots de substituição
wrapper: callable object to be updated, func: callable object serving as source
Colocados típicos
- functools.wraps (common alternative)
- decorator syntax
- inner wrapper functions
Substituições comuns
- functools.wraps(wrapper) — simpler
- preserves more attributes automatically
- manual attribute copying — flexible but error-prone
- using __wrapped__ — exposes original function but less common
Erros comuns
Swapping argument order (update_wrapper(func, wrapper)) — cause: misunderstanding parameter order — consequence: wrapper loses its own attributes and fails to copy source metadata; Omitting the func argument — cause: forgetting to pass source function — consequence: AttributeError when accessing copied attributes; Applying update_wrapper to a class instead of a function — cause: treating class as callable — consequence: TypeError or missing attribute copying; Not returning the wrapper from a decorator — cause: overlooking return statement — consequence: decorator returns None, breaking the decorated function; Using update_wrapper inside a nested decorator without preserving __wrapped__ — cause: chaining wrappers incorrectly — consequence: lost original function reference in introspection tools.
Similar / contraste
functools.wraps — decorator-friendly wrapper that calls update_wrapper internally; manual attribute copying — direct assignment of __module__, __name__, __doc__ without helper; __wrapped__ attribute — exposes original function but does not copy metadata
Interferências
Coming from Java: may expect method metadata to be preserved automatically → Python's update_wrapper must be called explicitly to copy __module__, __name__, __doc__
Família do chunk
- functools.wraps
- decorator pattern
- __wrapped__ attribute
Nuance
When NOT to use: if you do not need to preserve function metadata (e.g., internal helper decorators where introspection is irrelevant); Performance/resource implications: negligible overhead, microsecond-level cost per call; Boundary conditions: only works on callable objects; missing attributes in the source are not set; chaining wrappers may accumulate __wrapped__ links.
Efeito pragmático
Using update_wrapper correctly ensures decorated functions retain correct metadata, enabling reliable introspection, documentation, debugging, and compatibility with tools that rely on __name__, __doc__, __module__, and __annotations__.
Dica de memória
Like a photocopier that preserves all the original document's stamps and signatures when making a copy.
Nota
Returns the wrapper object, allowing chaining; available since Python 2.5; in Python 3.4+ also copies __annotations__ by default.
Log in to save chunks.