Author phr
Recipients phr
Date 2009-10-16.19:24:01
SpamBayes Score 3.79837e-08
Marked as misclassified No
Message-id <1255721043.52.0.906494418992.issue7153@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2009-10-16 19:24:03phrsetrecipients: + phr
2009-10-16 19:24:03phrsetmessageid: <1255721043.52.0.906494418992.issue7153@psf.upfronthosting.co.za>
2009-10-16 19:24:02phrlinkissue7153 messages
2009-10-16 19:24:01phrcreate