no side effects

Andrew Koenig ark at
Wed Jan 8 16:37:25 CET 2003

Michele> I was surprised by the following code:
Michele>     for i in [1,2,3]:
Michele> ...     print i,
Michele> ...     i=3

Michele> I would have expected only 1 to be printed, but instead Python
Michele> continues the loop without noticing that the value of i has
Michele> changed. IOW, no side effect.
Michele> I am not against that (I am even in favor), but it fooled my 
Michele> intuition coming from other languages.

What would your intuition have said if you had written this instead?

             for i in [3, 1, 4]:
                 print i,

Andrew Koenig, ark at,

More information about the Python-list mailing list