I have a function that allow many options as boolean values, e.g.:
def my_func(req, option_a=False, option_b=True, option_c=True):
pass
I want to make sure that boolean values are added for these variables, so I can do a simple check as such:
def my_func(req, option_a=False, option_b=True, option_c=True):
for param in [option_a, option_b, option_c]:
if not isinstance(param, bool):
raise ValueError(f'Unexpected value {param} for X. A boolean value is required')
Obviously this does not give a lot of information about which argument has the wrong value. I was wondering, therefore, if it is possible to get the name of the variable so that X
in the above snippet is replaced by the variable name. An example Error would be:
Unexpected value bananas for option_b. A boolean value is required
Is this possible in Python?