no side effects

Peter Hansen peter at engcorp.com
Wed Jan 8 18:11:41 EST 2003


Nagy László wrote:
> 
> Another explanation for a newbie would be this.
> 
> When comparing C++ (or other similar language) and Python for loop,
> the first thing I would mention is that in C++, a for loop is terminated
> by a condition
> but in Python, there is no condition anyway.
> 
> So in this program:
> 
>  >>>>for i in [1,2,3]:
>  > ...     print i,
>  > ...     i=3
> 
> or even in this one:
> 
>  >>>>for i in [1,1,1]:
>  > ...     print i
>  > ...     i = 1
> 
> of course the loop won't be terminated after the first
> iteration (no reason to do that).

Excellent answer!  And it emphasizes or at least focuses
attention on the fact that for simply iterates over a 
sequence of items, whether they be numbers, strings, or
object of other kinds:

for i in [3, 'test', SomeObject(), ('a', 'tuple')]:
    print i
    i = 3

-Peter




More information about the Python-list mailing list