On Thu, Apr 16, 2009 at 7:07 PM, Raymond Hettinger python@rcn.com wrote:
I think you're focusing on just one solution, one that involves piling-up too many extensions in one function that should be dirt simple. There are many other approaches: try/except, wrap the input in a default itertool, use all(), use next(it, default) to test the first value, etc.
Right, I've now grown used to wrapping it in try/except in advance just to be on the safe side; in the past I had to go back and fix it after it has already bombed with a ValueError once. So yes, overall I find the proposed parameter a handy convenience, not a fix to a glaring omission.
I think what it boils down to is how exceptional is the empty iterable for the overall task at hand. Does what follows the min/max computation differ substantially when the input is empty from when it's not, or does the following code can remain blissfully ignorant about how was the value determined ? In the former case, raising an exception is a feature since it forces the user to think about this case and handle it explicitly. If not, then it's just extra boilerplate we could do without.
A good example of the first case is division by zero: typically the course of action is totally different if some intermediate computation involves division by zero; you can't just replace it with some default and continue with the rest computations as if nothing happened (although a default might make sense as the "final" result, depending on the application). OTOH, code that uses dict.get() or getattr() with a default doesn't really care whether the key/attribute is actually in the queried dict/object, the following logic remains the same.
So the bottom line is, are most use cases of taking the min/max of an empty iterable closer to these that involve an intermediate DivisionByZero or to those that involve a missing key/attribute ? I don't have a general answer, both make sense under different circumstances.
George
PS: Thanks for the default() recipe, seems generally useful, not only for min/max.