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

Taught in

References