Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call function without optional arguments if they are None

There's a function which takes optional arguments.

def alpha(p1="foo", p2="bar"):      print('{0},{1}'.format(p1, p2)) 

Let me iterate over what happens when we use that function in different ways:

>>> alpha() foo,bar >>> alpha("FOO") FOO,bar >>> alpha(p2="BAR") foo,BAR >>> alpha(p1="FOO", p2=None) FOO,None 

Now consider the case where I want to call it like alpha("FOO", myp2) and myp2 will either contain a value to be passed, or be None. But even though the function handles p2=None, I want it to use its default value "bar" instead.
Maybe that's worded confusingly, so let me reword that:

If myp2 is None, call alpha("FOO"). Else, call alpha("FOO", myp2).

The distinction is relevant because alpha("FOO", None) has a different result than alpha("FOO").

How can I concisely (but readably) make this distinction?

One possibility would usually be to check for None within alpha, which would be encouraged because that would make the code safer. But assume that alpha is used in other places where it is actually supposed to handle None as it does.

I'd like to handle that on the caller-side.

One possibility is to do a case distinction:

if myp2 is None:     alpha("FOO") else:     alpha("FOO", myp2) 

But that can quickly become much code when there are multiple such arguments. (exponentially, 2^n)

Another possibility is to simply do alpha("FOO", myp2 or "bar"), but that requires us to know the default value. Usually, I'd probably go with this approach, but I might later change the default values for alpha and this call would then need to be updated manually in order to still call it with the (new) default value.

I am using python 3.4 but it would be best if your answers can provide a good way that works in any python version.


The question is technically finished here, but I reword some requirement again, since the first answer did gloss over that:
I want the behaviour of alpha with its default values "foo", "bar" preserved in general, so it is (probably) not an option to change alpha itself.
In yet again other words, assume that alpha is being used somewhere else as alpha("FOO", None) where the output FOO,None is expected behaviour.

like image 996
lucidbrot Avatar asked Sep 25 '18 08:09

lucidbrot


People also ask

How do you call a function without an argument?

Defining a Function Without Parameters We can call the function by typing its name followed by parentheses () . When we call this function, it will print the current date. Note that, this output can be different for you. The output will be the date in which you call the function.

Are function call arguments optional?

The number of values we specify in a function call must be equal to the number of arguments that a function accepts. This is unless we use optional arguments. In which case, we can specify fewer arguments than a function accepts because some arguments are optional.

Can function work without arguments?

Functions do not have declared return types. A function without an explicit return statement returns None . In the case of no arguments and no return value, the definition is very simple. Calling the function is performed by using the call operator () after the name of the function.

How do you pass an optional argument in Python?

Users can either pass their values or can pretend the function to use theirs default values which are specified. In this way, the user can call the function by either passing those optional parameters or just passing the required parameters. Without using keyword arguments. By using keyword arguments.


1 Answers

Pass the arguments as kwargs from a dictionary, from which you filter out the None values:

kwargs = dict(p1='FOO', p2=None)  alpha(**{k: v for k, v in kwargs.items() if v is not None}) 
like image 163
deceze Avatar answered Sep 17 '22 01:09

deceze