Programmer A writes the following function:
def compute_value(threshold = sys.float_info.max):
...
return value
which has the optional parameter threshold which in a natural way has the maximum float value as a default to represent "no threshold".
Programmer B has also a representation for a threshold, but uses None to represent the absence of threshold. Unfortunately the compute_value function does not raise any exception if threshold=None but gives the incorrect answer. Hence a bug when programmer B passes None as threshold.
I would say that the best solution is to change the function in
def compute_value(threshold = None):
if threshold is None:
threshold = sys.float_info.max
...
return value
since this function is more general than before, as it handles the None value in a way which makes sense.
This poses the question: is it always best to use only None as default values?
It is not the first time that functions having default parameters different from None cause trouble to me. In other situations I was finding myself to remove None values from a kwargs dictionary...
Another related (maybe silly) question. Actually programmer B modified the function above as follows:
def compute_value(threshold = sys.float_info.max):
if threshold is None:
threshold = sys.float_info.max
...
return value
which is perfectly correct but seems bad to me. Bad because sys.float_info.max is repeated twice... but: is this a violation of the DRY principle? Because, strictly speaking, also None is repeated twice in the first implementation and both None and sys.float_info.max are constants.