Skip to content

Commit 28fad82

Browse files
committed
Add example of a decorator that takes arguments
1 parent 23e0b36 commit 28fad82

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

source-code/decorators/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@ What is it?
1414
large. The decorated function in this example is the factorial.
1515
Also it illustrates `functools` wrap decorator to retain the wrapped
1616
function's name, docstring, etc.
17+
1. `decorator_arguments.py`: an example of a decorator that takes arguments.
18+
The `check_range` decorator takes a minimum and maximum value as arguments
19+
and checks if the wrapped function's argument falls within that range,
20+
throwing an exception if it does not.
1721
1. `memoize.py`: an example of adding a cache to a function using a simple
1822
custom decorator, as well as `functools`'s `lru_cache` decorator.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python3
2+
3+
import functools
4+
5+
6+
def check_bounds(min_value, max_value):
7+
'''Check bounds of a function argument
8+
9+
Parameters
10+
----------
11+
min_value: float
12+
Smallest value allowed for the function's parameter
13+
max_value: float
14+
Largest value allowed for the function's parameter
15+
16+
Raises
17+
------
18+
ValueError:
19+
If the argument is outside the specified bounds
20+
'''
21+
def check_bounds_wrapper(_func):
22+
@functools.wraps(_func)
23+
def wrapper(x):
24+
if min_value > x or x > max_value:
25+
raise ValueError(f'argument {x} not in [{min_value}, {max_value}]')
26+
return _func(x)
27+
return wrapper
28+
return check_bounds_wrapper
29+
30+
31+
@check_bounds(min_value=-1.0, max_value=1.0)
32+
def silly(x):
33+
'''Compute a rather uninteresting function
34+
35+
Parameters
36+
----------
37+
x: float
38+
Argument for the function
39+
40+
Returns
41+
-------
42+
float:
43+
Value computed by the function
44+
'''
45+
return x**2 - 2.0
46+
47+
48+
if __name__ == '__main__':
49+
print(silly(0.5))
50+
try:
51+
print(silly(2.5))
52+
except ValueError as e:
53+
print(f'Excepton raised as expected: {e.message}')

0 commit comments

Comments
 (0)