Can't get around "IndexError: list index out of range"

Fredrik Lundh fredrik at
Tue Oct 10 18:22:47 CEST 2006

MonkeeSage wrote:

> In Libs/, lines 302-306:
>             try:
>                 for i in range(lineno, lineno + self.MAXLINES):
>                     print self.__lines[i]
>             except IndexError:
>                 break
> With my proposal, that could be written as:
>             for i in range(lineno, lineno + self.MAXLINES):
>                 if self.__lines.has_index(i):
>                     print self.__lines[i]
>                 else:
>                     break
> Granted, in this particular case the amount of code is not reduced, but
> (and I would hope you'd agree) the control flow is certainly easier to
> follow.

so to "improve" a piece of code that's been optimized for the common 
case, you're adding an extra method call and a test to the inner loop?

and this because you think Python programmers don't understand try-
except statements ?

I think we can all safely ignore you now.


More information about the Python-list mailing list