Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python -- Only pass arguments if the variable exists

Tags:

I have the following variables that a user can optionally submit through a form (they are not required, but may do this to filter down a search).

color = request.GET.get ('color') size = request.GET.get ('size') 

Now I want to pass these variables to a function, but only if they exist. If they do not exist I want to just run the function without arguments.

the function without arguments is:

apicall = search () 

with color only it's

apicall = search (color) 

and with color and size it's

apicall = search (color, size) 

If the argument is defined I want to pass it to the function, but if it's not I do not want to pass it.

What is the most efficient way to do that? Does python have built-in methods for this?

like image 291
user1328021 Avatar asked May 15 '13 23:05

user1328021


People also ask

Are Python arguments passed by reference or value?

All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function.

How do you pass an optional argument in Python?

You can define Python function optional arguments by specifying the name of an argument followed by a default value when you declare a function. You can also use the **kwargs method to accept a variable number of arguments in a function.

Can you pass unnamed arguments into a function in Python?

Yes. You can use *args as a non-keyword argument.

What is optional argument in Python?

In Python, when we define functions with default values for certain parameters, it is said to have its arguments set as an option for the user. Users can either pass their values or can pretend the function to use theirs default values which are specified.


2 Answers

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).

like image 69
Henry Keiter Avatar answered Sep 18 '22 12:09

Henry Keiter


In python 3 you could pack them up in a list, filter it, and use the *-operator to unpack the list as arguments to search:

color = request.GET.get ('color') size = request.GET.get ('size') args = [ arg for arg in [color, size] if arg ] search(*args) 

Note, however, if color is falsy and size is truthy, you would be calling search with 1 argument being the value of size, which would probably be wrong, but the original question doesn't mention desired behaviour in that case.

(necromancing since I was looking for a better solution than mine, but found this question)

like image 41
AdamAL Avatar answered Sep 19 '22 12:09

AdamAL