Btw related question, Why it was decided that list comprehension variables are scoped?
On Mon, 17 Jan 2022 at 9:06 PM John Sturdy <jcg.stu...@gmail.com> wrote: > This is clearly one for me to abandon! Thanks for the explanations. > > > John > > On Mon, Jan 17, 2022 at 7:04 PM Paul Moore <p.f.mo...@gmail.com> wrote: > >> On Mon, 17 Jan 2022 at 18:51, John Sturdy <jcg.stu...@gmail.com> wrote: >> > >> > My idea is to support the style in which each variable comes into >> existence at a single well-defined point, whatever path is taken through >> the code, and so in the case of that example, it would be encouraging the >> use of conditional expressions. >> >> But that's a style that Python explicitly rejected right back when it >> was first designed, and it's fundamental to the language that you >> *don't* explicitly declare variables. So sorry, but it's never going >> to happen[1]. >> >> If you want to be really precise, technically "the style in which each >> variable comes into existence at a single well-defined point" is >> actually what Python already does. The "well-defined point" is the >> start of the scope (function or class definition) where the variable >> is used, though, and not at some new "let" keyword. >> >> To see this, look at the following: >> >> >>> def f(): >> ... print(n) >> ... n = 1 >> ... >> >>> f() >> Traceback (most recent call last): >> File "<stdin>", line 1, in <module> >> File "<stdin>", line 2, in f >> UnboundLocalError: local variable 'n' referenced before assignment >> >> The variable "n" exists when the print statement runs (it's unbound, >> but it exists - that's what the exception says), and that's because >> the variable comes into existence at the *start* of the scope >> (function). >> >> And the choice to only have a limited number of ways to define scopes >> is *also* a fundamental design principle of Python, so arguing that >> suites should each define their own scope won't get you very far >> either. >> >> Essentially, you're proposing to change Python to no longer be Python, >> in some fairly fundamental ways. Don't be surprised if you get very >> few supporters for your idea... >> >> Paul >> >> [1] In theory, the SC could say that such a change is fine, but I >> can't see that ever happening. >> >
_______________________________________________ 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/CXK5M2M6SPBXHMTBAQP4AMCGN3BETRXA/ Code of Conduct: http://python.org/psf/codeofconduct/