Meaning
A higher-order function that wraps another function to log its call signature (function name, positional args, keyword args) before delegating to the original function.
Primary Function
Logs function invocation details for debugging or tracing purposes.
Communicative Purpose
Communicates that a function is being called with specific arguments, aiding in debugging and tracing execution flow.
Pattern
def logger(func):\n def wrapper(*args, **kwargs):\n print(f'Calling {func.__name__} with args={args}, kwargs={kwargs}')\n return func(*args, **kwargs)\n return wrapper
Core Structure
def logger(func):\n def wrapper(*args, **kwargs):\n print(f'Calling {func.__name__} with args={args}, kwargs={kwargs}')\n return func(*args, **kwargs)\n return wrapper
Função primária
Logs function invocation details for debugging or tracing purposes.
Propósito comunicativo
Communicates that a function is being called with specific arguments, aiding in debugging and tracing execution flow.
Situações de gatilho
When a developer wants to trace function calls without modifying the original function's source code.
Contextos
Used in debugging, profiling, logging frameworks, or any scenario where function call tracing is needed.
Padrão
def logger(func):\n def wrapper(*args, **kwargs):\n print(f'Calling {func.__name__} with args={args}, kwargs={kwargs}')\n return func(*args, **kwargs)\n return wrapper
Estrutura central
def logger(func):\n def wrapper(*args, **kwargs):\n print(f'Calling {func.__name__} with args={args}, kwargs={kwargs}')\n return func(*args, **kwargs)\n return wrapper
Slots de substituição
{"slot":"func","description":"The function to be wrapped and logged."} {"slot":"*args","description":"Positional arguments passed to the wrapped function."} {"slot":"**kwargs","description":"Keyword arguments passed to the wrapped function."}
Colocados típicos
- logger decorator wrapper *args **kwargs func.__name__ print
Substituições comuns
- {"slot":"func"
- "alternatives":["f"
- "func_to_log"
- "target"]} {"slot":"*args"
- "alternatives":["*args"
- "*params"]} {"slot":"**kwargs"
- "alternatives":["**kwargs"
- "**kwds"]}
Erros comuns
Forgetting to return func(*args, **kwargs) which would break the wrapped function's return value. Using print instead of a proper logging framework in production code. Not preserving function metadata (e.g., __name__, __doc__) – can be fixed with functools.wraps.
Similar / contraste
{"chunk":"functools.wraps","contrast":"Preserves metadata of the wrapped function; logger does not unless wraps is used."} {"chunk":"property decorator","contrast":"Used for attribute access control, not for logging calls."} {"chunk":"contextmanager decorator","contrast":"Manages resource setup/teardown via __enter__/__exit__, not call logging."}
Interferências
May interfere with functions that rely on introspection of their own arguments if args/kwargs are altered. Using print in production can clutter stdout; prefer the logging module.
Família do chunk
- function decorator
- logging decorator
- wrapper pattern
Nuance
The logger decorator is a simple debugging aid; for production logging, consider using the logging module and functools.wraps to preserve function metadata.
Efeito pragmático
Enables transparent insertion of logging behavior without altering the original function's code, promoting separation of concerns.
Dica de memória
Imagine a 'logger' as a spy that watches function calls and reports what it sees.
Nota
Remember to return the wrapper function; otherwise the decorator returns None.
Upgrade path
Consider using functools.wraps and the logging module for a production-ready logging decorator.
Log in to save chunks.