Loop-and-a-half (Re: Curious assignment behaviour)
Michael Hudson
mwh at python.net
Fri Oct 12 04:51:42 EDT 2001
Donn Cave wrote:
> I am not a big fan of the for i in range() idiom, myself. But you
> aren't required to use it. If you want the effect of
>
> for (i = 0; i<n; i++) {
> --- do stuff --
> }
>
> then in Python, you write
>
> i = 0
> while i < n:
> --- do stuff --
> i = i + 1
>
> "wc" says that's 7 more characters to type, which would be a concern
> if you have to engrave it on linoleum or something. And you have to
> avoid "continue".
i = 0
while i < n:
try:
--- do stuff ---
finally:
i += 1
('prolly only works in relatively recent Pythons)
Anyone using this as an idiom deserves a slap, though.
> It seems to me this is the loop construct that people
> must use, though, if they're going to expect the tricks they learn
> in C to work.
Such people already have major problems, no?
> Python's "for" loop is really a different thing, not
> something I ever found all that cutesy-poo, just a sequence thing
> that you see in plenty of languages but not C.
It is subtly changing in 2.2, of course.
Cheers,
M.
More information about the Python-list
mailing list