A Python module that simplifies the creation of decorators, ensuring they preserve the original function's signature. Current version: 5.2.1, released on February 24, 2025. Maintained by Michele Simionato. Requires Python 3.8 or higher.
pip install decoratorVerified import paths — ran on the pinned version, not inferred.
An example demonstrating how to define and use a decorator with the 'decorator' module.
Use 'from decorator import decorator' to correctly import the function.
Ensure your Python environment is version 3.8 or higher before using the 'decorator' module.
It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
Install the library using pip: `pip install decorator`
Ensure you are using `decorator.decorate` correctly to create the decorator, or that your custom decorator function returns a callable. For example, to create a simple decorator preserving signature: `from decorator import decorator
@decorator
def my_decorator(func, *args, **kwargs):
print('Before function call')
result = func(*args, **kwargs)
print('After function call')
return result
@my_decorator
def my_function():
return 'Hello'`Ensure that the expression after `@` is a valid callable (e.g., a function name, `module.function_name`, or a function call that returns a callable). Complex expressions like `@(lambda f: f)` or chained calls like `@f()()` are not allowed directly. You might need to assign the result of a complex expression to a variable first, then use that variable as the decorator. The `decorator` library's `decorate` function is designed to handle more complex programmatic decorator creation, but the `@` syntax itself has strict rules. Example of incorrect vs. correct usage:
```python
# Incorrect
# @(lambda f: f)
# def my_func(): pass
# Correct (assign lambda to a name first)
identity_decorator = lambda f: f
@identity_decorator
def my_func():
pass
# If using 'decorator' library to build a decorator factory:
from decorator import decorator
def my_decorator_factory(arg):
@decorator
def my_actual_decorator(func, *args, **kwargs):
print(f'Decorator arg: {arg}')
return func(*args, **kwargs)
return my_actual_decorator
@my_decorator_factory('test')
def another_func():
return 'World'
```No dependency data recorded yet.