Menu
Coddy logo textTech

Practice #4

Lesson 11 of 12 in Coddy's Python Decorators course.

challenge icon

Challenge

Medium

Write 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 ValueError

You can assume:

  • All functions decorated with restricted_range use only numbers as arguments.
  • All functions decorated with restricted_range do not use **kwargs just *args.

Try it yourself

All lessons in Python Decorators