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 NaNs, and if it’s anything else, we replace NaNs 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?
Usually people use
Noneas the default value and then check foris not None.If you need to allow
None, too, use a dummy object: