[Python-Dev] Re: PEP 326 now online (original) (raw)

Jeff Epler jepler at unpythonic.net
Tue Jan 27 08:32:43 EST 2004


On Mon, Jan 26, 2004 at 04:47:43PM -0800, Josiah Carlson wrote:

This results in the simplification of the (0, None) examples into one, but introduces an index variable and sequence lookups for the general case:

def findminGeneral(seq): if not len(seq): raise TypeError("Sequence is empty") cur = seq[0] for i in xrange(1, len(seq)): cur = min(seq[i], cur) return cur def findminMax(seq): if not len(seq): raise TypeError("Sequence is empty") cur = Max for obj in seq: cur = min(obj, cur) return cur Now they both have the same number of lines, but I find the second one a bit clearer due its lack of sequence indexing.

I'd avoid the sequence indexing by writing the following: def findmin_Iter(seq): seq = iter(seq) cur = seq.next() for i in seq: cur = min(i, cur) return cur This code will raise StopIteration, not TypeError, if the sequence is empty.

Jeff



More information about the Python-Dev mailing list