Skip to content Skip to sidebar Skip to footer

How To Check In Python That At Least One Of The Default Parameters Of The Function Specified

What is the best practice in python to check if at least one of the default parameters of the function is specified? Let's suppose we have some function: def some_function(arg_a=No

Solution 1:

You could use all to check if they all equal None and raise the ValueError:

ifall(v isNonefor v in {arg_a, arg_b}):
    raise ValueError('Expected either arg_a or arg_b args')

this gets rid of those if-elif clauses and groups all checks in the same place:

f(arg_a=0) # ok    
f(arg_b=0) # ok
f()        # Value Error  

Alternatively, with any():

ifnotany(v isnotNonefor v in {arg_a, arg_b}):
    raise ValueError('Expected either arg_a or arg_b args')

but this is definitely more obfuscated.

In the end, it really depends on what the interpretation of pythonic actually is.

Solution 2:

Depends on what you expect as values for arg_a and arg_b, but this is generally sufficient.

if not arg_a and not arg_b:
    raise ValueError(...)

Assumes that arg_a and arg_b are not both booleans and cannot have zeros, empty strings/lists/tuples, etc. as parameters.

Depending on your needs, you can be more precise if you need to distinguish between None and 'falsies' such as False, 0, "", [], {}, (), etc.:

if arg_a isNoneand arg_b isNone:
    raise ValueError(...)

Solution 3:

You may consider using kwargs if you have quite a number of such named arguments with mismatching default values:

def some_function(**kwargs):
    reqd = ['arg_a', 'arg_b']
    if not all(i in kwargs for i in reqd):
        raise ValueError('Expected either {} args'.format(' or '.join(reqd)))

    arg_a = kwargs.get('args_a')
    arg_b = kwargs.get('args_b')
    arg_c = kwargs.get('args_c', False)

Solution 4:

To check if at least one of the default parameters of the function specified

The solution using locals, dict.values and any functions:

defsome_function(arg_a=None, arg_b=None, arg_c=False):
    args = locals()
    if (any(args.values()) == True):
        print('Ok')
    else:
        raise ValueError('At least one default param should be passed!')

some_function(False, 1)            # Ok
some_function('text', False, None) # Ok
some_function(0, False, None)      # Error

https://docs.python.org/2/library/functions.html#any

Solution 5:

Since you're proposing such a pattern, there's possibly some unifying meaning to arg_a, arg_b and arg_c. If that's so, you could ...

fromenum import EnumclassGroup(Enum):
    A = auto()
    B = auto()
    C = auto()

def some_function(val: int, t: Group):
    if t == Group.A:
        # do stuff with val
    elif t == Group.B:
        # do other stuff with val
    elif t == Group.C:
        # do other stuff with val

some_function(1, Group.A)

Here the caller is forced to specify both a value, and what the value corresponds to. This is tbh a bit of a hack around the lack of proper enumeration support in python and I've no idea if it's pythonic. It will however be picked up by a type checker.

Post a Comment for "How To Check In Python That At Least One Of The Default Parameters Of The Function Specified"