On 27/11/20 12:11 am, Paul Sokolovsky wrote:

On Thu, 19 Nov 2020 18:53:01 +1300
Greg Ewing <greg.ew...@canterbury.ac.nz> wrote:

Note that this is *not* the same as introducing a new scope.

And that's very sad. That means instead of solving the root of the
problem, adhoc workarounds are proliferated.

I'd be open to the idea of a variation on the for loop
that does introduce a new scope for the loop variable.
It would need new syntax, though.

I didn't suggest it initially because proposals for
subscopes within functions typically meet with a lot
of resistance, and I didn't want to get sidetracked
arguing about that when it isn't strictly needed.

Million cells for million of iterations?

If your million iterations create a million nested
functions that capture the variable and are all kept
alive, then yes, you will get a million cells along
with your million function objects.

If the loop doesn't have any nested functions, or
they don't capture the loop variable, there will be
0 cells.

Note that the cells would still be needed even if
there were a nested scope.

I'd suggest that it should be "for let"

That makes no sense as a phrase in English.

the scoping should be optimized to always treat "x" (as
the "for" control variable) by-value,

That would make it very different from the way
closures work anywhere else in the language.

for const x in some_iterator:
     ...
>
> That will make it obvious that "x" is captured by value,
>
> and that:
>
> for const x in some_iterator:
>      ...
>      x = 1
>
> Doesn't make sense (should lead to error - assigning to a const).

This is mixing up two different issues -- the scope
of the loop variable, and whether it can be assigned
to.

I proposed "new" because it directly gets at the
important thing, which is that x is a *different* x
each time round the loop.

So, there shouldn't be "special syntax". There should be generic syntax
to solve Python's block scoping and const'ness issues.

Which people would then have to remember to use.
By "special" in that context I mean "something you
have to do differently to get your for loop to work
the way you expect". Not necessarily something that
can only be used with for loops.

--
Greg
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/BN2UZHSB6FFH3HQJ7JUBW3PK7AQEAVOZ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to