no side effects

holger krekel pyth at devel.trillke.net
Wed Jan 8 12:32:20 EST 2003


"Martin v. L?wis" wrote:
> holger krekel wrote:
> > Depends on your background.  *If* somebody e.g. thinks that 
> > 
> >     for i in range(3):
> >         print i
> >         i = 3
> > 
> > is somewhat equivalent to C's
> > 
> >     int i;
> >     for (i=0 ; i<3 ; i++) {
> >         printf("%d\n", i);
> >         i = 3;
> >     }
> > 
> > then IMHO my explanation serves well and talking about underlying 
> > iterator protocols wouldn't help too much.  
> 
> Difficult to say, since you have to come up with a theory of why people 
> think so. 

Actually i wanted to study psychology but did computer science instead :-)

Of course, there are all kinds of analogies for all kinds of constructs. 
I happened to guess that the OP was thinking along the above lines. 
And I didn't mean to say that the above "equivalence" is canonical 
or natural in any way. 

> People might expect that the Python for loop translates to 
> something like
> 
> seq = [1,2,3]
> i = seq[0]
> while 1:
>    SUITE
>    try:
>      i = seq[seq.find(i)+1]
>    except IndexError:
>      break
> 
> Of course, they would never spell it out that way: they might think "At 
> the end of the loop, look at the current value of i, then take the 
> element from the sequence that follows i". This is roughly how the C 
> loop works, and translates into the Python fragment above.
> [further explanation]

ok.

> > Why is the way the python for-loop gets to the next *value* 
> > more important than understanding that python works with 
> > name-object bindings everywhere?  And what is so wrong 
> > about this (*)? 
> 
> I would expect that Michelle has grasped the notion of namespaces and 
> name bindings by now; the question hinted at a misunderstanding of the 
> for loop semantics.

Maybe, who knows? 

pythons "name binding" often confuse newcomers, too. 
So i choose to explain both the loop and name-binding 
concept.  In my first response i wrote

        for i in [1,2,3]:
            print i
            i=3

    the for-loop will iteratively bind the (local) name to
    1, then 2, then 3.  It never looks at the binding of
    the name 'i'.  With 'i=3' you only change the binding
    of 'i' to the object '3' but the for-loop will blindly
    change the binding again.

How does this give any wrong impression about the for-loop
in question?  

In my experience, explanations are often better if they also
transport appropriate nomen clatura.  In order to say what
the for-loop actually does to 'i'  it occurs to me that
name-binding plays a central role.  

Explaining the details of the actual iteration over the list
didn't seem as central to me. 

> This details of the for loop is hardly ever noticed, 
> but I'm sure it has confused more than one newcomer (I remember having 
> been confused by it myself).

i agree. The loop and iteration details deserve attention.  

regards,

    holger





More information about the Python-list mailing list