Default value for optional argument in Python:

arguments, python

Solution

Usually people use `None` as the default value and then check for `is not None`.

If you need to allow `None`, too, use a dummy object:

__default = object()
def get_data(replace_nan=__default):
    if replace_nan is __default:
        ...

Problem

I have the following method: ``` def get_data(replace_nan=False): if replace_nan is not False data[numpy.isnan(data)] = replace_nan return data else: return data[~numpy.isnan(data)] ``` So, if `replace_nan` is False, we return some data array but remove `NaN`s, and if it's anything else, we replace `NaN`s with the argument. Problem is, I may want to replace `NaN` with `False`. Or anything else, for that sake. What's the most pythonic way to do so? This: ``` def get_data(**kwargs): if "replace_nan" in kwargs: ... ``` works, but is semantically ugly (because we're really just interested in one keyword argument, `replace_nan`) Any suggestions how to handle this case?

Original source