PEP 276 Simple Iterator for ints

Peter Hansen peter at engcorp.com
Thu Nov 29 04:33:27 CET 2001


James_Althoff at i2.com wrote:
> 
> Peter Hansen wrote:
> >What am I missing here?  Is the above not just
> >a far less sane way of writing the following?
> >
> >if index >= len(mylist):
> >    print 'index out of range'
> 
> But "sane" is *such* a loaded term. ;-)

Loaded in this case with connotations of adequate
performance. ;)

>     index in range(len(mylist))
> 
> works both in a for-loop and an if-statement.  I see it quite a lot in code
> that I come across.  YMMV.

Except that when used with a list, it performs a brute-force
search for index (for the 'if' case)!  Surely that wouldn't
be very sane.

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list