Meaning
Copies attributes like __module__, __name__, and __qualname__ from a 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
Explain how to retain original function attributes when wrapping a function.
Core Structure
functools.update_wrapper(wrapper, func, assigned=tuple, updated=tuple)
Função primária
Preserve function metadata when creating wrapper functions (e.g., decorators).
Propósito comunicativo
Explain how to retain original function attributes when wrapping a function.
Situações de gatilho
When writing a decorator or wrapper that should not obscure the wrapped function's identity for debugging, help, or serialization.
Contextos
Used in decorator factories, for web frameworks, logging decorators that decorators, middleware wrappers, or any function‑wrapping scenario where introspection (e.g., __name__, __module__) must stay intact.
Estrutura central
functools.update_wrapper(wrapper, func, assigned=tuple, updated=tuple)
Slots de substituição
wrapper: any callable wrapper object; func: the original function to copy attributes from; assigned: tuple of attribute names to copy; updated: tuple of attribute names to update (e.g., '__dict__').
Colocados típicos
- functools
- wraps
- decorator
- wrapper
- __module__
- __name__
- __qualname__
- __dict__
Substituições comuns
- functools.wraps (which internally calls update_wrapper with default args).
Erros comuns
Forgetting to copy __module__ or __qualname__, leading to misleading tracebacks; swapping wrapper and func arguments.
Similar / contraste
functools.wraps (decorator form), functools.update_wrapper (function form), manual attribute copying.
Interferências
Do not confuse update_wrapper with wraps; the former returns the wrapper, the latter returns a decorator.
Família do chunk
- functools.wraps
- functools.update_wrapper
- decorator patterns
Nuance
Only copies the attributes listed in assigned/updated; any other attributes remain unchanged on the wrapper.
Efeito pragmático
Preserves introspection metadata, making decorated functions behave like the original for help(), logging, and serialization.
Dica de memória
Think of update_wrapper as ‘copy the name tag’ from the original function to your wrapper.
Nota
The updated tuple is empty here, meaning no attributes are updated (only assigned).
Upgrade path
Learn functools.wraps for a more convenient decorator form.
Log in to save chunks.