unintuitive for-loop behavior

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Mon Oct 3 01:15:36 EDT 2016


Steve D'Aprano writes:

> On Sun, 2 Oct 2016 05:35 pm, Jussi Piitulainen wrote:
>
> [...]
>>> I'm sorry, I don't understand what you mean by "Python semantics" versus
>>> "a translation". A translation of what? In what way is it "magical
>>> semantics"? I see nothing magical in your code: it is Python code.
>> 
>> "Python semantics" is the semantics that Python actually uses when it
>> updated the variables of a for-loop. I would like to call it "assignment
>> semantics".
>> 
>> The second function is an executable translation of the first function
>> under a different semantics. I would like to call it "binding semantics"
>> to distinguish it from "assignment semantics", but that doesn't work
>> when people insist that "binding" and "assignment" are the same thing in
>> Python, so I called it "magical semantics" instead.
>
> Why shouldn't people say that binding and assignment are the same
> thing in Python? What's the difference?

Outside Python, (lambda x : f(x)) is said to "bind" x. It's different
from assigning a new value to x. It's similar to how quantifiers in
logic are said to bind their variables, so that variables bound to
different quantifiers are different variables.

[...]

>>> def fabulate2(m, n):
>>>     # The simple, Pythonic, non-complicated way.
>>>     for i in range(m):
>>>         print(i, end = ': ')
>>>         c = 0
>>>         for j in range(n):
>>>             print(j, end = ', ' if j + 1 < n else ' : ')
>>>             c += 1
>>>         print(i, c)
>> 
>> It misses only the point of the exercise.
>
> Perhaps that's because your point wasn't clear.

Perhaps.

>>>> A summary of sorts: it's possible to demonstrate the scope
>>>> difference in Python code, with no box in sight; boxes are
>>>> irrelevant; the relevant issue is what function and when the loop
>>>> variable is associated with, explicitly or implicitly.
>>>
>>> I don't know what "scope difference" you think you are
>>> demonstrating.  tabulate() has a single scope, fabulate() has
>>> multiple scopes because it has inner functions that take i as
>>> argument, making them local to the inner functions. Um, yeah, of
>>> course they are different. They're different because you've written
>>> them differently. What's your point?
>> 
>> The scope difference is the topic of this thread.
>> 
>> My point is that the for-loops could be translated/compiled/expanded
>> into the lower-level code so that the loop variables would be in
>> their own scopes.
>
> Well why didn't you say so?

I tried.

> Perhaps I missed it the first time, but the point of the exercise
> would have been so much more clear if only you had introduced it was
> an explanation like:
>
> "Here is a proof of concept that shows that Python could give for
> loops their own scope. If this Python code [nested for-loops] were
> translated mechanically by the compiler [nested while loops with
> iterators] then each loop would have their own scope."

Something like that but not quite that. The nonlocal declarations are
also important, but since they would not be visible in the code that the
programmer writes, it would be misleading to say that the loop has its
own scope.

In my original sketch perhaps "each loop variable is in its own scope",
and in the current version "the loop variable of each iteration step is
in its own scope"? Would those words work for you?

I would like to be able to say simply that the for-loop would "bind" its
variable, as opposed to assigning to it (and maybe update it by binding
as opposed to assignment), but this clashes with the way these terms are
used in the Python community.

> And you are absolutely right: boxes are irrelevant. I think that boxes
> only came into this with Greg's argument that there's a difference
> between "creating a new binding" versus "updating an existing one".

The frustrating thing is that Greg's posts make perfect sense to me, and
I agree with him about the difference.

How about "creating a new scope for the loop variable" versus "updating
the loop variable in the existing scope" then?

>> The code might not seem so obfuscated to you if you thought of it not
>> as source code but as compiled code. Except it's still Python. What
>> word should I use instead of "translation"? Would "transformation" be
>> understood?
>
> That might have helped.

Well. I think you understand now.



More information about the Python-list mailing list