enforce arguments to a specific list of values

What is the pythonic way to enforce a function to take a specific set of values for a given parameter? For instance there is a function like:

def results(status,data):

I want to restrict parameter 'status' to a set of values like 0, 1 or 99.

Asked By: user3885927
||

Answer #1:

You need to check the value inside the function:

def results(status, data):
    valid = {0, 1, 99}
    if status not in valid:
        raise ValueError("results: status must be one of %r." % valid)

Here, valid is a set, because the only thing we care about is whether status is a member of the collection (we aren't interested in order, for example). To avoid recreating the set each time you use the function, you'd probably define it as a "constant"1 global:

VALID_STATUS = {0, 1, 99}

def results(status, data):
    if status not in VALID_STATUS:
        raise ValueError("results: status must be one of %r." % VALID_STATUS)

Example usage:

>>> results(7, [...])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in results
ValueError: results: status must be one of {0, 1, 99}.

Always try to raise the most appropriate exception you can - ValueError tells the caller of the function what's going on better than Exception does, for example.


1 It's not really constant, but by convention, ALL_UPPERCASE variable names in Python are considered to be intended as constants.

Answered By: Zero Piraeus

Answer #2:

You can check within the function itself if status is a valid value and if it is not then raise an exception.

def results(status,data):
    list_valid_status = [0, 1, 99]
    # list_valid_status = (0, 1, 99) # could be a tuple so it doesn't get modified by accident
    if status not in list_valid_status:
        raise ValueError("Wrong status")
Answered By: jramirez
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .



# More Articles