Assuming that's a standard get
call (like on a dictionary), this ought to be easy. Define your function with None
for the defaults for your parameters, and then pass color
and size
without bothering to check them!
def apicall(color=None, size=None):
pass # Do stuff
color = request.GET.get('color')
size = request.GET.get('size')
apicall(color, size)
This way, you only check for None
arguments in one place (inside the function call, where you have to check anyway if the function can be called multiple ways). Everything stays nice and clean. Of course this assumes (like I said at the top) that your get
call is like a normal Python dictionary's get
method, which returns None
if the value isn't found.
Finally, I notice that your function name is apicall
: there's a chance you don't actually have access to the function code itself. In this case, since you may not know anything about the default values of the function signature and None
might be wrong, I would probably just write a simple wrapper to do the argument-checking for you. Then you can call the wrapper as above!
def wrapped_apicall(color=None, size=None):
if color is None and size is None:
return apicall()
# At least one argument is not None, so...
if size is None:
# color is not None
return apicall(color)
if color is None:
# size is not None
return apicall(size)
# Neither argument is None
return apicall(color, size)
NOTE: This second version shouldn't be necessary unless you can't see the code that you're calling and don't have any documentation on it! Using None
as a default argument is very common, so chances are that you can just use the first way. I would only use the wrapper method if you can't modify the function you're calling and you don't know what its default arguments are (or its default arguments are module constants or something, but that's pretty rare).
apicall (color, size)
will throw an error, won't it? – Demagogic