def my_decorator(func):\n def wrapper():\n print('before')\n result = func()\n print('after')\n return result\n return wrapper
Decorators & Metaclasses

Meaning

A simple decorator that prints a message before and after calling the wrapped function.

Primary Function

Wraps a function to add pre‑ and post‑execution side effects (printing).

Communicative Purpose

Illustrates the decorator pattern for adding behavior before and after a function call.

Pattern

def <decorator_name>(func):\n def wrapper(*args, **kwargs):\n <before>\n result = func(*args, **kwargs)\n <after>\n return result\n return wrapper

Core Structure

def <decorator_name>(func):\n def wrapper(*args, **kwargs):\n <before>\n result = func(*args, **kwargs)\n <after>\n return result\n return wrapper

Função primária

Wraps a function to add pre‑ and post‑execution side effects (printing).

Propósito comunicativo

Illustrates the decorator pattern for adding behavior before and after a function call.

Situações de gatilho

When you need to add logging, debugging, or other side‑effects around a function call without modifying its source.

Contextos

Used in Python code where cross‑cutting concerns such as logging, timing, or authentication are applied to functions.

Padrão

def <decorator_name>(func):\n def wrapper(*args, **kwargs):\n <before>\n result = func(*args, **kwargs)\n <after>\n return result\n return wrapper

Estrutura central

def <decorator_name>(func):\n def wrapper(*args, **kwargs):\n <before>\n result = func(*args, **kwargs)\n <after>\n return result\n return wrapper

Slots de substituição

<decorator_name>: name of the decorator; <before>: code to run before calling func; <after>: code to run after; func: the function being wrapped; *args, **kwargs: function arguments.

Colocados típicos

  • @my_decorator
  • @logger
  • @timer
  • @wrapper
  • @decorator

Substituições comuns

  • replace print with logging.info
  • add *args
  • **kwargs to wrapper
  • use functools.wraps to preserve metadata
  • return result
  • handle exceptions.

Erros comuns

forgetting to return the wrapper; omitting *args/**kwargs causing errors on functions with arguments; swallowing exceptions; not returning the function result.

Similar / contraste

similar to functools.wraps, class‑based decorators, context managers; contrasted with plain function calls and inheritance‑based extension.

Interferências

Omitting *args/**kwargs leads to TypeError when the wrapped function expects arguments; forgetting to return result makes the decorated function return None; not preserving __name__ and docstring can hinder debugging.

Família do chunk

  • Python decorators
  • function wrappers
  • decorator patterns

Nuance

This simple decorator only works for functions without arguments; real‑world decorators must accept *args, **kwargs to be generic.

Efeito pragmático

Signals that the decorated function is being instrumented for side effects such as logging or timing.

Dica de memória

Prints 'before' and 'after' around a function call.

Upgrade path

functools.wraps, class‑based decorators, decorators that accept arguments

Tag de espaçamento: Medium-term

Log in to save chunks.