Sequence-length - Missing the obvious ?
Michael Hudson
mwh21 at cam.ac.uk
Sat Jun 3 05:16:28 EDT 2000
aahz at netcom.com (Aahz Maruch) writes:
> In article <3938090C.9FB5D057 at stud.ntnu.no>,
> Peter Schneider-Kamp <peter at schneider-kamp.de> wrote:
> >mlauer at trollinger-fe.rz.uni-frankfurt.de wrote:
> >>
> >> But why ? How does the for loop knows
> >> how much elements the sequence contains ?
> >> I did not program the __len__ function,
> >> so... how ?
> >
> >As far as I understand the for loop goes on until an IndexError is
> >raised. Then the error is cleared and the for loop ends.
>
> Let's be technically precise here: an exception is raised and caught by
> the "for" handler; the for loop is then broken (precisely as if the
> break statement was executed). Like any other caught exception, the
> exception is thrown away (not "error is cleared").
Not quite "precisely as if the break statement was executed", wrt else
blocks. Ponder the output of these two statements:
for i in range(10):
print i
else:
print "else"
for i in range(10):
if i > 4: break
print i
else:
print "else"
but otherwise, yes.
Cheers,
M.
--
The ability to quote is a serviceable substitute for wit.
-- W. Somerset Maugham
More information about the Python-list
mailing list