PEP 276 Simple Iterator for ints

Bjorn Pettersen BPettersen at NAREX.com
Thu Nov 29 13:56:43 EST 2001


> From: Kerim Borchaev [mailto:warkid at storm.ru] 
> 
> Hello James,
> Thursday, November 29, 2001, 5:18:43 AM, you wrote:
> Jic> Peter Hansen wrote:
> >>Or just:
> >>
> >>if not 0 <= index < len(mylist):
> >>    print 'index out of range'
> 
> Jic> Right.  Which has the added advantage of not repeating index.
> 
> Why not just _use_ that index on the list to check whether it's valid?
> 
> try:
>     mylist[index]
> except IndexError:
>     print 'index out of range'

Because a negative index doesn't give an IndexError, and presumably we
wanted to make sure the list was accessed through a positive index...

-- bjorn




More information about the Python-list mailing list