[Python-ideas] Have max and min functions ignore None
Franklin? Lee
leewangzhong+python at gmail.com
Mon Dec 28 23:08:49 EST 2015
What do people think about having `max` and `min` ignore `None`?
Examples:
max(1, None) == 1
min(1, None) == 1
max([1, None]) == 1
max(None, None) == max() or max(None, None) == max([])
(The last one currently throws two different errors.)
This change would allow one to use `None` as a default value. For example,
def my_max(lst):
best = None
for x in lst:
best = max(best, x)
return best
Currently, you would initialize `best` to the first element, or to
float('-inf').
There are more complicated examples, which aren't just replications of
`max`'s functionality. The example I have in mind wants to update
several running maximums during iteration.
I know that there are other ways to do it (having given one above).
What if this becomes _the_ obvious way to do it?
I'm concerned about this silencing some bugs which would have been
caught before. I'm also worried about whether it would make sense to
people learning Python. I'm less concerned about custom types which
allow comparisons to `None`, because I don't understand why you would
want that, but you can change my mind.
More information about the Python-ideas
mailing list