I would like to know what the best practice is regarding default values for functions.
Let's say I have a function:
def my_function(x, **kwargs):
kwargs_default = {'boolean_offset': False}
kwargs_default.update(kwargs)
if kwargs_default['boolean_offset']:
x += 100
return x
It is just a quick example and does not have any other meaning.
my_function(2)
will return 2
. my_function(2, boolean_offset=True)
will return 102
.
The point is that I have a variable called boolean_offset
that is turned off by default, but may be turned on by the user.
In my real problem I have a function with many input variables. Often not all of these input variables are used and in most cases users want to use the default settings. To make the code more readable I would like to use *args
and **kwargs
. Further I would like the potentially used variables to have default values, which can be overwritten by the user.
Is the code in my example the best way to do this?