no side effects

Nobody nobody at nowhere.near.here
Wed Jan 8 08:35:18 EST 2003


One assigment happens in the "for i" statement. If you move i=3 before the
print statement
    for i in [1,2,3]:
        i = 3
        print i

it will print the 3 three times.
"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 am not against that (I am even in favor), but it fooled my
> intuition coming from other languages.
> Maybe others will appreciate that.
>
> Cheers,
>
> --
> Michele Simionato - Dept. of Physics and Astronomy
> 210 Allen Hall Pittsburgh PA 15260 U.S.A.
> Phone: 001-412-624-9041 Fax: 001-412-624-9163
> Home-page: http://www.phyast.pitt.edu/~micheles/






More information about the Python-list mailing list