
On 1/17/22 7:33 AM, John Sturdy wrote:
One of the few things I don't like about Python is that variable scope continues after the end of the suite the variable was created in --- probably because I think of local variable declarations as equivalent to lambdas that are called immediately, which works for most modern programming languages as well as for the theory. 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.
That's a feature, not a bug. A very common Python pattern is to set such common variables using if/else or try/except, and then to use those variables further along in the code *outside* the suite that set them.
I think a way to address this that would be more compatible with Python's style would be to add a new declaration syntax, perhaps borrowing from other languages and using the keyword 'let' where the variable is first assigned (and the rest of the assignment / declaration being as before). The variables declared this way would exist up to the end of the suite in which they were declared;
The syntax might be optional, but the change in semantics is not. If that change was made, then every Python programmer would have to keep track of which variables were created with `let` and then also track the lifespan of those variables which are now different from other variables in that same function or class scope. I'm sorry if programming in Python is not a choice you get to make, but that change is a huge jump in complexity for a near-zero gain. -- ~Ethan~