Is there a way in Python to pass optional parameters to a function while calling it and in the function definition have some code based on "only if the optional parameter is passed"
5 Answers
The Python 2 documentation, 7.6. Function definitions gives you a couple of ways to detect whether a caller supplied an optional parameter.
First, you can use special formal parameter syntax *. If the function definition has a formal parameter preceded by a single *, then Python populates that parameter with any positional parameters that aren't matched by preceding formal parameters (as a tuple). If the function definition has a formal parameter preceded by **, then Python populates that parameter with any keyword parameters that aren't matched by preceding formal parameters (as a dict). The function's implementation can check the contents of these parameters for any "optional parameters" of the sort you want.
For instance, here's a function opt_fun which takes two positional parameters x1 and x2, and looks for another keyword parameter named "optional".
>>> def opt_fun(x1, x2, *positional_parameters, **keyword_parameters):
... if ('optional' in keyword_parameters):
... print 'optional parameter found, it is ', keyword_parameters['optional']
... else:
... print 'no optional parameter, sorry'
...
>>> opt_fun(1, 2)
no optional parameter, sorry
>>> opt_fun(1,2, optional="yes")
optional parameter found, it is yes
>>> opt_fun(1,2, another="yes")
no optional parameter, sorry
Second, you can supply a default parameter value of some value like None which a caller would never use. If the parameter has this default value, you know the caller did not specify the parameter. If the parameter has a non-default value, you know it came from the caller.
4 Comments
positional_parameters is a tuple, and keyword_parameters a dictionary.formal parameters preceded by * (what is this called?) from a keyword parameter? As I understood it, keyword parameters are specified using the = operator in the function call (so opt_fun(1,2, optional="yes") provides a keyword argument "yes" for a function that anticipates, but doesn't necessarily require, the parameter option).formal parameters preceded by * as simply "the caller-provided arguments that doesn't correspond to positional arguments that goes before any keyword arguments". For example, in opt_fun(1,2, "blah", 3.14, mykey="yes", myyear="2018"), "blah" and 3.14 are forma arguments preceded by * because it's in between the two positional arguments and the argument that uses the = sign. The resulting tuple positional_parameter would be the duple: ("blah", 3.14). Is this correct?parameter" not "parameters" for "preceded by *". The language allows for only one *identifier in the parameter list. b) read docs.python.org/2/reference/…, c) read docs.python.org/2/reference/expressions.html#calls . Sorry, running out of characters permitted in this comment.def my_func(mandatory_arg, optional_arg=100):
print(mandatory_arg, optional_arg)
http://docs.python.org/2/tutorial/controlflow.html#default-argument-values
I find this more readable than using **kwargs.
To determine if an argument was passed at all, I use a custom utility object as the default value:
MISSING = object()
def func(arg=MISSING):
if arg is MISSING:
...
4 Comments
default_value=100; def my_func(mandatory_argument, optional_argument_with_default_value=default_value): ...def op(a=4,b=6):
add = a+b
print add
i)op() [o/p: will be (4+6)=10]
ii)op(99) [o/p: will be (99+6)=105]
iii)op(1,1) [o/p: will be (1+1)=2]
Note:
If none or one parameter is passed the default passed parameter will be considered for the function.
2 Comments
op(b=2)If you want give some default value to a parameter assign value in (). like (x =10). But important is first should compulsory argument then default value.
eg.
(y, x =10)
but
(x=10, y) is wrong
3 Comments
*arg **karg as function arguments. search on google.You can specify a default value for the optional argument with something that would never passed to the function and check it with the is operator:
class _NO_DEFAULT:
def __repr__(self):return "<no default>"
_NO_DEFAULT = _NO_DEFAULT()
def func(optional= _NO_DEFAULT):
if optional is _NO_DEFAULT:
print("the optional argument was not passed")
else:
print("the optional argument was:",optional)
then as long as you do not do func(_NO_DEFAULT) you can be accurately detect whether the argument was passed or not, and unlike the accepted answer you don't have to worry about side effects of ** notation:
# these two work the same as using **
func()
func(optional=1)
# the optional argument can be positional or keyword unlike using **
func(1)
#this correctly raises an error where as it would need to be explicitly checked when using **
func(invalid_arg=7)
2 Comments
_NO_DEFAULT = object().help(func) you get a clearer idea how the argument is interpreted if not passed explicitly.