def restrict_range(minimum=None, maximum=None, default=None, type_=int):

    def restricted(*args, **kwargs):
        if default is not None and not (args or kwargs):
            return default
value = type_(*args, **kwargs)
if minimum is not None and value < minimum or maximum is not None and value > maximum:
    raise ValueError
return value
return restricted
Extending Python’s int type to accept only values within a given range