Should for loops run in their own scope?

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-l... 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

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 <steve@pearwood.info> wrote:

In the example at the top of the discuss thread
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 <steve@pearwood.info> wrote:
-- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython

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 <steve@pearwood.info> wrote:

In the example at the top of the discuss thread
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 <steve@pearwood.info> wrote:
-- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython
participants (3)
-
Christopher Barker
-
David Mertz, Ph.D.
-
Steven D'Aprano