no side effects

Andrew Koenig ark at research.att.com
Wed Jan 8 10:37:25 EST 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,
                 i=3


-- 
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark




More information about the Python-list mailing list