[Python-ideas] max() allowed to take empty list
Tom Pinckney
thomaspinckney3 at gmail.com
Tue Apr 13 03:36:40 CEST 2010
Many functions are happy to take empty lists:
>>> map(lambda x: x, [])
[]
>>> filter(lambda x: x == 1, [])
[]
>>> sum(a)
0
>>> reduce(lambda x,y: x+y, [], 0)
0
But not max():
>>> max([])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: max() arg is an empty sequence
Allowing max to return None would make a lot of functional style programming easier, instead of explicitly having to check for empty lists or ignoring exceptions.
None makes sense as a return value in many contexts. For example, hypothetical code like:
if max(values) >= 90: return True # works even if values is []
thought not every case:
a / max(values) # can't divide by None if values is []
More information about the Python-ideas
mailing list