Meaning
Applies a decorator named `example` to a function, passing a list (or other expression) as configuration; often used to attach sample data or test cases to a function for testing or demonstration.
Primary Function
Decorator application / test case annotation
Communicative Purpose
Attach sample input data or configuration to a function for testing or demonstration.
Pattern
@example(; )
Core Structure
@example(; )
Função primária
Decorator application / test case annotation
Propósito comunicativo
Attach sample input data or configuration to a function for testing or demonstration.
Situações de gatilho
Writing unit tests that need sample inputs, creating demo functions with predefined data, labeling functions with example values for documentation.
Contextos
Testing suites, educational code snippets, internal libraries that provide custom decorators for behavior-driven development.
Padrão
@example(; )
Estrutura central
@example(; )
Slots de substituição
decorator_argument: expression (commonly a list literal)
Colocados típicos
- def function_name():
- assert statements
- test frameworks
Substituições comuns
- @example([4
- 5
- 6])
- @example([])
- @example({"key": "value"})
Erros comuns
Omitting parentheses, using a non-callable decorator, forgetting to define the `example` decorator, passing incompatible argument types.
Similar / contraste
@pytest.mark.parametrize (provides multiple test cases per argument) vs @example (single configuration); @staticmethod (no argument) vs decorator with arguments.
Interferências
Coming from Java: expecting annotations like @Override that cannot take arguments; in Python decorators are functions that can accept parameters.
Família do chunk
- decorator patterns
- test annotation patterns
- parameterized testing
Nuance
The decorator must be defined in scope; if not, a NameError occurs. The argument is evaluated at decoration time, so avoid expensive computations. The decorated function receives the same signature unless the decorator modifies it.
Efeito pragmático
Makes test intent explicit and centralizes example data, improving readability and maintainability.
Dica de memória
Think of @example as labeling a function with its sample input.
Nota
The example decorator shown here is a simplified illustration; real-world test frameworks like pytest provide more sophisticated decorators such as @pytest.mark.parametrize for parameterized testing.
Upgrade path
Using @example with multiple arguments or a class‑based decorator for more complex configuration.
Log in to save chunks.