Honestly, the construct of `lambda x=x: stuff(x)` in a loop or
comprehension isn't that hard to learn.

Yes, I've also forgotten it and tripped over that. But then, I've also
banged my head on the wall when I use a language with block scope and
forget that variables won't be available after the block ends.

Learning Python scope rules is just part of learning the language.
Simplicity and consistency is much more important to me than "yet another
way to do it."

On Sun, Sep 12, 2021, 11: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/
>
_______________________________________________
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/JVAI5OSACTGML2UR6BUOUZ7KUZUTYCTM/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to