no side effects
John Roth
johnroth at ameritech.net
Wed Jan 8 09:29:22 EST 2003
"Michele Simionato" <mis6 at pitt.edu> wrote in message
news:2259b0e2.0301080510.3b2e5d4f at posting.google.com...
> I was surprised by the following code:
>
> >>> for i in [1,2,3]:
> ... print i,
> ... i=3
>
> I would have expected only 1 to be printed, but instead Python
> continues the loop without noticing that the value of i has
> changed. IOW, no side effect.
I'm not certain how you have it coded; indentation tends
to be strange in a newsgroup posting.
If the print and assignment are indented, I would expect
it to print all three integers, one after the other.
If they are not indented, then I would expect the behavior
you got: the for loop executed to completion with a
null body, and then the print statement executed
with the final value of "i".
HTH
John Roth
More information about the Python-list
mailing list