
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. A worse case of what can be done with the scope rules for variables declared as they are now is that some code paths define the variable and some don't, so later on in the program it might or might not be defined (I have seen this done, and find it takes extra mental work to work with). I was thinking of all blocks defining scopes, like they do in C and many other languages. It would be possible to add another statement type just for this, I suppose, and have "let x = y in:" and a suite under it. On Mon, Jan 17, 2022 at 5:24 PM Christopher Barker <pythonchb@gmail.com> wrote:
On Mon, Jan 17, 2022 at 7:36 AM John Sturdy <jcg.sturdy@gmail.com> wrote:
For example, it bugs me that you can write:
if a: b = f(x) else: b = g(y)
and get the same variable 'b' from it in the lines of code following that, regardless of which path was taken.
Really? But that is a VERY common pattern. In fact, I would find it very hard to write an enormous amount of code without it. and what if you refactored that to be:
b = f(x) if a else g(x)
Do you really want that to mean something different ?!?!
Or are what you are getting at that if you want those two forms to be the same, then you'd need to do something like:
b = None if a: b = f(x) else: b = g(y)
Which means essentially that you'd like to have to declare names in order to ues them outside the very narrowest of scopes.
But this is not how Python works, and it never has, it would be a really massive change, and I for one, would not like it :-(
Also -- which blocks would create a new scope? All of them:
for (else) while (else) with if (elif, else) try (except, else)
I guess what I'm getting at is that I'm pretty sure I would find this useful rarely, and an annoyance often.
- CHB
-- Christopher Barker, PhD (Chris)
Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython