Decorator
PronunciationDEK-uh-ray-ter
Definition
A function that wraps another function to add behavior, such as logging or permission checks, without editing the original code.
Where you hear it
Python (@login_required), TypeScript frameworks like NestJS, and middleware talk.
Examples
Add
@login_requiredso only signed-in users can open the page.We wrote a decorator that logs how long each call takes.
The cache decorator stores the result of the function for ten minutes.
Common mistake
In Python, forgetting functools.wraps, which makes the wrapped function lose its name and docstring.
Don't confuse with
A decorator wraps a function to modify its behavior dynamically, while inheritance creates a new subclass to extend functionality statically.
Say it at work
- In a meeting or chat
Can we write a custom decorator to handle the caching for these API endpoints?
- In an email or ticket
Please use the authentication decorator on the new routes to ensure proper access control.
Keep exploring
More in Programming Fundamentals