[New-bugs-announce] [issue7153] add "start" arg to max and min functions
paul rubin
report at bugs.python.org
Fri Oct 16 21:24:03 CEST 2009
New submission from paul rubin <phr at users.sourceforge.net>:
Lots of times I want to find the largest element of a list or sequence,
defaulting to 0 if the list or sequence is empty. max(seq) throws an
exception if seq is empty, so I end up using reduce(max, seq, 0). That
is a standard functional programming idiom but can be a bit confusing to
imperative-style Python programmers.
max with multiple args is already overloaded to mean the maximum of the
args, so I think it would be a good fix to add a keyword arg to accept
an optional initial value: max(seq, start=0). For symmetry, min should
accept the same arg.
The alternatives to using reduce aren't so attractive. If seq happens
to be a list, there might be a temptation to conditionalize on
len(seq)==0, but that is poor style since it will break if seq later
changes to an arbitrary sequence. And trying to test it by calling
.next() and saving the value and/or trapping StopIteration gets awfully
messy.
----------
components: Library (Lib)
messages: 94145
nosy: phr
severity: normal
status: open
title: add "start" arg to max and min functions
versions: Python 2.5
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7153>
_______________________________________
More information about the New-bugs-announce
mailing list