decorator
A line starting with @ placed above a function or class that wraps it to add behaviour or information.
In more detail
A decorator is a function that takes another function or class and returns a wrapped version, so behaviour such as caching, logging or registration is added without editing the original. Python and TypeScript both use the @name syntax.
Examples
@functools.lru_cache(maxsize=None) adds memoisation to a recursive function.In this course: pytest uses decorators to attach metadata to tests and fixtures, such as @pytest.fixture, @pytest.mark.slow and @pytest.mark.parametrize. The decorated function is still an ordinary function underneath.@pytest.fixture(scope="module")class PaymentService { @logged process(amount: number) { } }Related terms
- functools.lru_cache — A Python decorator from the functools module that memoises a function, remembering results for arguments it has seen.
- property — A named value belonging to an object; in languages such as C# and Python, it can also be a member that looks like a field but runs code when read or written.
- dataclass — A class decorated with @dataclass from the dataclasses module, which writes __init__, __repr__ and __eq__ for you from a list of annotated fields.
- fixture — A named piece of setup, defined with @pytest.fixture, that a test receives by listing the fixture's name as a parameter.
- marker — A label attached to a test with @pytest.mark.name so you can select, skip or configure tests by that label.
- parametrized test — One test function run many times with different data, each run reported as a separate test case.
- ClassMethodDecoratorContext — The typed object passed as the second argument to a standard method decorator, describing the method being decorated.
- experimentalDecorators — The compiler option that enables TypeScript's older, non-standard decorators used by frameworks such as Angular.
- TC39 — The Ecma committee that designs JavaScript, moving each new feature proposal through numbered stages from 0 to 4.
- wrapper function — A function that calls another function, adding behaviour before or after it, such as timing, retries or logging.
- cross-cutting concern — A need that applies across many services or endpoints, such as authentication, rate limiting, logging or TLS, rather than to one feature.
Taught in
- Dynamic Programming: Memoisation And Tabulation — Algorithms & Data Structures for Working Engineers
- Methods, Attributes And self — Hands-on Python: Complete Video Course & Book
- Dunder Methods, And Making Objects Pythonic — Hands-on Python: Complete Video Course & Book
- The Test Pyramid And Where It Is Wrong — Automated Testing, TDD & Quality Engineering
- Parametrised Tests — Automated Testing, TDD & Quality Engineering
- Fixtures And Their Scope — Automated Testing, TDD & Quality Engineering
- Function Utilities: ReturnType, Parameters And Constructor — Production TypeScript
- The satisfies Operator: Safe Validation Without Widening — Production TypeScript
- Modern Decorators: Stage 3 Standards And Class Metadata — Production TypeScript
- The Spine Artifact: Generating Typed Models From OpenAPI — Production TypeScript