In the example at the top of the discuss thread

>>> larr = [lambda: i for i in range(10)]
>>> iarr = [l() for l in larr]
>>> iarr
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9]

a comprehension is used, which already (kinda) creates it's own scope. So
that is really about lambda, not loops.

Or at least, that post was about lambda.

And lambda works just like other functions, which is should -- are folks
suggesting lambda should be different?

(side note: I think there is a contsant struggle because folks *want*
lambda to be more than it is in python. And since lambda only supports a
single expression, and we now have comprehensions, its use is pretty darn
limited anyway.)

As for for loops:

I personally have used the fact that for loops "leak" their scope more
often than I get bitten them not having their own scope.

-CHB


On Sun, Sep 12, 2021 at 8:38 AM Steven D'Aprano <st...@pearwood.info> wrote:

> Over on Discuss, there's a small discussion about the "lambdas in for
> loop" problem described in the FAQs.
>
> https://discuss.python.org/t/make-lambdas-proper-closures/10553
>
>
> https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result
>
> I've created a poll on Discuss. If you have an account, you may like to
> vote on whether for loops should run in their own scope:
>
> https://discuss.python.org/t/should-loops-be-in-their-own-scope-poll/10593
>
>
> * No change, leave loops as they are.
>
> * Change loops to use their own scope.
>
> * No change for loops by default, but add an option to run them in a new
> scope.
>
>
> Just throwing out some random syntax to be shot down, how does this read
> to folks?
>
>     for item in sequence in scope:
>         # block here has its own scope
>         ...
>
>
> I have no idea what the implementation difficulties might be, but with
> lots of hand-waving and in full knowledge that it won't be *me* having
> to do the implementation, I imagine it could be easy enough if the for
> loop is compliled into a nested function that is then immediately
> executed. Sort of like a comprehension?
>
> Personally, I'm happy with Python not using block scopes (only functions
> and classes create a new scope, other blocks do not). But for loop scope
> does seem to be frequently requested, so while I don't see the benefit
> myself, maybe others do.
>
>
> --
> Steve
> _______________________________________________
> 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/YCTRSKPVDE43LAYNVDMXI7OJJ73FZDGN/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
_______________________________________________
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/EUIVWCVZNDYY7UPHEEHXEDYD7GYBFA3W/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to