no side effects
Michele Simionato
mis6 at pitt.edu
Wed Jan 8 15:25:16 EST 2003
holger krekel <pyth at devel.trillke.net> wrote in message news:<mailman.1042039926.12943.python-list at python.org>...
> "Martin v. L?wis" wrote:
> > holger krekel wrote:
> > > 'i' might not be local but usually it is. When answering
> > > newcomer questions i try to refrain from talking in language
> > > lawyer details (as much as possible). I only tried to explain the
> > > for-loop from the viewpoint of names bound to objects. I didn't
> > > want to compare iteration protocols between python and C++ just
> > > to explain that.
> >
> > I'm uncertain though whether an actually incorrect explanation helps the
> > newcomer.
>
> Of course, it shouldn't be incorrect. I don't think i actually was.
>
> > Namespaces are totally irrelevant for this feature of the for loop.
>
> 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.
>
> 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 (*)?
>
> slightly irritated,
>
> holger
>
>
> (*) see http://www.python.org/dev/doc/devel/ref/naming.html
> for further details.
If I may defend Holger, I had in mind exactly the C example he wrote.
The namespace argument maybe of not relevance here (yes, it is better
to think of i as a variable local to the loop), nevertheless it has
be useful to me to be reminded that Python interprets "variables"
in a quite difference sense from C. I tend to forget it sometimes.
I think I am not the only one who can be fooled by this example.
Cheers,
Michele
P.S. I forgot to post the output of the script, which is 1 2 3.
And yes, the indentation is correct.
More information about the Python-list
mailing list