Meaning
Copies selected attributes (__module__, __name__, __qualname__, __doc__, __annotations__) from the original function to a wrapper function, preserving metadata for debugging and introspection.
Primary Function
Preserve function metadata when creating wrapper functions (e.g., decorators).
Communicative Purpose
Indicates that the wrapper should retain the original function's identity and documentation.
Core Structure
functools.update_wrapper(wrapper, func, assigned=tuple_of_attributes)
Função primária
Preserve function metadata when creating wrapper functions (e.g., decorators).
Propósito comunicativo
Indicates that the wrapper should retain the original function's identity and documentation.
Situações de gatilho
When writing a decorator that wraps a function and you want the wrapper to appear as the original function for introspection, help(), or logging.
Contextos
Used inside decorator definitions, especially when creating wrappers that need to mimic the wrapped function's metadata.
Estrutura central
functools.update_wrapper(wrapper, func, assigned=tuple_of_attributes)
Slots de substituição
wrapper func assigned
Colocados típicos
- functools wraps decorator __module__ __name__ __qualname__ __doc__ __annotations__
Substituições comuns
- functools.wraps manual attribute copying
Erros comuns
Forgetting to copy __annotations__, losing type hints; using functools.wraps incorrectly by omitting the function argument.
Similar / contraste
functools.wraps (a convenience wrapper that calls update_wrapper internally) manual attribute copying (error-prone, verbose)
Interferências
Using update_wrapper without assigned attributes may copy too much (e.g., __dict__) causing unexpected shared state.
Família do chunk
- functools
- decorators
- function metadata
Nuance
The assigned tuple controls which attributes are copied; omitting __annotations__ loses type hints in Python 3.x.
Efeito pragmático
Signals to readers and tools that the wrapper is transparent with respect to the wrapped function's interface.
Dica de memória
Think of 'updating the wrapper's resume' with the original function's details.
Nota
functools.wraps is a convenience function that internally calls update_wrapper with a default set of attributes.
Upgrade path
functools.wraps (more convenient decorator form)
Log in to save chunks.