Meaning
A decorator wrapper that preserves the original function's metadata (name, docstring, annotations) using functools.wraps.
Primary Function
Preserves the wrapped function's metadata when creating a decorator.
Communicative Purpose
Indicates that the wrapper maintains the wrapped function's signature and documentation.
Pattern
@functools.wraps(func)\ndef wrapper(*args, **kwargs):\n return func(*args, **kwargs)
Core Structure
@functools.wraps(func)\ndef wrapper(*args, **kwargs):\n return func(*args, **kwargs)
Função primária
Preserves the wrapped function's metadata when creating a decorator.
Propósito comunicativo
Indicates that the wrapper maintains the wrapped function's signature and documentation.
Situações de gatilho
When writing a decorator that should not obscure the wrapped function's identity for debugging, introspection, or help.
Contextos
Used when implementing decorators for logging, timing, authentication, caching, etc., in Python.
Padrão
@functools.wraps(func)\ndef wrapper(*args, **kwargs):\n return func(*args, **kwargs)
Estrutura central
@functools.wraps(func)\ndef wrapper(*args, **kwargs):\n return func(*args, **kwargs)
Slots de substituição
func: the function being wrapped; *args, **kwargs: arguments forwarded to the original function.
Colocados típicos
- functools
- wraps
- decorator
- wrapper
- *args
- **kwargs
- func
Substituições comuns
- func can be any callable
- *args
- **kwargs can be renamed but are conventionally kept.
Erros comuns
Forgetting to return func(*args, **kwargs); forgetting to import functools; applying wraps incorrectly (e.g., missing parentheses).
Similar / contraste
Similar to a plain wrapper without wraps (loses metadata); contrasted with class-based decorators or using functools.wraps on a class.
Interferências
Forgetting to import functools; applying wraps to a class instead of a function may not preserve all attributes.
Família do chunk
- functools.wraps
- decorator pattern
- function wrapper
Nuance
Preserves __name__, __doc__, __module__, and annotations; does not preserve a changed signature if the wrapper alters *args/**kwargs (though the call signature remains the same).
Efeito pragmático
Signals to readers and tools that the wrapper is transparent regarding the wrapped function's interface.
Dica de memória
@functools.wraps(func) def wrapper(*args, **kwargs): return func(*args, **kwargs)
Nota
functools.wraps is a convenience wrapper for updating wrapper function attributes.
Log in to save chunks.