Python Decorator Syntax
(Redirected from @ Decorator)
Jump to navigation
Jump to search
A Python Decorator Syntax is a Python Language Feature that modifies functions and classes using @ symbol notation.
- AKA: Python Decorator, Decorator Pattern in Python, @ Decorator, Python Annotation.
- Context:
- It can typically wrap Python Functions with additional functionality without modifying bodies.
- It can typically implement Cross-Cutting Concerns like logging, caching, and authentication.
- It can typically support Decorator Chaining by stacking multiple decorators on single targets.
- It can typically accept Decorator Parameters for configurable behavior and customization.
- It can often enhance Code Reusability by separating concerns from core logic.
- It can often simplify Metaprogramming through declarative syntax and function transformations.
- It can often work with Class Methods, Static Methods, and Property decorators.
- It can range from being a Simple Decorator to being a Parameterized Decorator, depending on its configuration needs.
- It can range from being a Function Decorator to being a Class Decorator, depending on its target type.
- It can range from being a Built-in Decorator to being a Custom Decorator, depending on its source.
- It can range from being a Stateless Decorator to being a Stateful Decorator, depending on its state management.
- ...
- Example(s):
- Built-in Python Decorators:
- @property for property access.
- @staticmethod for static methods.
- @classmethod for class methods.
- @functools.cache for memoization.
- Common Decorator Patterns:
- Timing decorator: @timer to measure execution time.
- Logging decorator: @log to record function calls.
- Validation decorator: @validate to check arguments.
- Framework Decorators:
- Flask: @app.route() for URL routing.
- Django: @login_required for authentication.
- Pytest: @pytest.fixture for test fixtures.
- ...
- Built-in Python Decorators:
- Counter-Example(s):
- Function Call, which invokes rather than modifies functions.
- Inheritance, which extends classes through subclassing not decoration.
- Monkey Patching, which modifies at runtime without decorator syntax.
- See: Python Language Feature, Decorator Pattern, Metaprogramming, Function Wrapper, Aspect-Oriented Programming, Python Function, Python Class.