Python - pass arguments only if variable exists

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

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

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

function without arguments:

 apicall = search () 

with color only this

 apicall = search (color) 

and with color and size

 apicall = search (color, size) 

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

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

+15
source share
5 answers

Assuming a standard get call (like in a dictionary), this should be easy. Define your function with None for the default parameters for your parameters, and then pass color and size without bothering to check!

 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 None arguments in one place (inside a function call, where you still need to check if the function can be called in several ways). Everything stays beautiful and clean. Of course, this assumes (as I said above) that your get call is similar to the regular Python get dictionary method, which returns None if no value is found.

Finally, I noticed that your apicall function apicall : there is a chance that you actually do not have access to the function code itself. In this case, since you may not know anything about the default values ​​for the function signature, and None may be incorrect, I will probably just write a simple wrapper to check the arguments. You can then invoke the shell as described 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 is not needed if you do not see the code that you are calling and you do not have documentation! Using None as the default argument is very common, so there is a chance that you can just use the first method. I would only use the wrapper method if you cannot change the function you are calling and you do not know what its default arguments are (or its default arguments are modular constants or something, but this is quite rare).

+11
source

No matter how much I like the @HenryKeiter solution , Python provides a MUCH simpler way to check for parameters. There are actually several different solutions.

  1. For example, if search() is a standalone function and you want to view the arguments, you can use the inspect module, as shown in this solution .

Code Example 1

 >>> import inspect >>> print(inspect.getfullargspec(search)) ArgSpec(args=['size', 'color'], varargs=None, keywords=None, defaults=(None, None)) 
  1. However, if search() is a class method (we'll call it Search ), then you can simply use the built-in vars function to see all the class methods and their parameters:

Code Example 2

 >>> import Search >>> print(vars(Search)) mappingproxy({'__init__': <function Search.__init__(self, size, color)>, 'search': <function Search.search(self, size, color)}) 

The only caveat associated with the second method is that it is more useful as a visual control tool than software, although you can technically say if 'size' in vars(Search)['search']: do something . It simply would not be very reliable. It’s easier and harder to say if 'size' in inspect.getfullargspec(Search.search).args: do something or for arg in inspect.getfullargsspec(Search.search).args: do something

+1
source

meybe you can use something like this:

 try: apicall = search (color, size) else: apicall = search(color) 
0
source

In Python 3, you can pack them into a list, filter it and use * -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, that if color false and size correct, you would call search with 1 argument equal to size , which would probably be wrong, but the original question does not mention the desired behavior in this case.

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

0
source

When you define your method, if you specify a default argument, you can specify the argument or not.

 def search(color=None, size=None): pass 

Then, when you call it, you can specify the keyword argument of your choice. Both of them would be valid:

 apicall = search(color=color) apicall = search(size=size) 
-1
source

Source: https://habr.com/ru/post/945109/


All Articles