Unexpected result.
John J. Lee
jjl at pobox.com
Sat Sep 25 08:31:11 EDT 2004
Paul Rubin <http://phr.cx@NOSPAM.invalid> writes:
> "Larry Bates" <lbates at swamisoft.com> writes:
> > Actually the result is exactly as expected. Programming 101 teaches
> > us not to reuse loop variables in nested loops.
>
> Programming 101 usually doesn't say whether a nested loop introduces a
> new scope or not. If there's a new scope, it's not re-use of a variable.
Whether the OP's usage is "re-use of a variable" is a matter of how
you choose to define the words in that phrase, I suppose. But even in
a hypothetical Python-like language that works the way Grzegorz
expected, whatever you choose to call the usage of the name i in G's
example, I call it "a bad idea".
I assume you didn't mean to imply that the OP's example wouldn't be
better if written with a differently-named loop variable, even in such
a language?:
>>> for i in ('a','b','c'):
... for j in (1,2,3):
... print j,
... print i,
...
1 2 3 a 1 2 3 b 1 2 3 c
John
More information about the Python-list
mailing list