Reputation: 1428
Is there a convention for raising errors if the proper combination of key-word arguments is not provided in a function call?
As an example:
def convert(miles=0, kilometers=0):
if miles and not kilometers:
return miles * 1.61
elif kilometers and not miles:
return kilometers / 1.61
else:
raise #exception
In this function, the one or the other parameter must receive an argument; if there are zero or two arguments, the function is invalid.
The built-in exceptions do not include an exception that is obvious for this situation. The option I have considered is TypeError
(used in other bad function calls). Is this the standard way to handle this situation or should I use a different exception?
By the way, I saw this question, but this is not what I am looking for.
Upvotes: 0
Views: 322
Reputation: 1024
ValueError
Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception such as IndexError.
https://docs.python.org/2/library/exceptions.html#exceptions.ValueError
One more thing...
If a function only has keyword arguments, users might confuse it as something they can call without specifying anything. Here you have no way of knowing which conversion they have in mind. You must have at least 2 positional arguments to calculate the values, or you can make two separate functions with one argument each.
I would recommend this:
def miles2km(miles=1):
return miles * 1.60934
def km2miles(km=1):
return 1.0/miles2km(1.0/km) if km else 0
The reason why I prefer this is because only one function needs to be maintained, since the conversion factor is only present in one of them.
Upvotes: 2