Practice #4
Lesson 11 of 12 in Coddy's Python Decorators course.
Challenge
MediumWrite a decorator named restrict_range that restricts the input arguments of a function to a specific range.
The decorator should take two parameters indicating the minimum and maximum values allowed for the input arguments. If the input arguments are outside of the specified range, the decorator should raise an exception - ValueError("Input argument out of range").
Here's an example usage of the decorator:
@restrict_range(0, 10)
def add(a, b):
return a + b
res = add(6, 5) # returns 11
res = add(3, 11) # raises ValueErrorYou can assume:
- All functions decorated with
restricted_rangeuse only numbers as arguments. - All functions decorated with
restricted_rangedo not use**kwargsjust*args.
Try it yourself