Lack of block scope

What is the suggestion here? Changing how scoping rules work or introducing a new mechanism?

Again, so? You’re showing a difference, not a problem.

1 Like

The point is that assigning a value to the enclosing scope would be tedious, as demonstrated with an inner function scope, which behaves similarly to the hypothetical block scope of control statements.

The solution is simple: use a different, preferably meaningful, name.

1 Like

Introducing block scope for while-loops, for-loops and if-elif-else statements.

Can you see the problem of having a globally-scoped variable being manipulated inside the local-scope of a for-loop, then further manipulated outside the local-scope of a for-loop?

Yes, that is the problem. It is easy to make the mistake of using a variable inside the local scope of a for-loop with the same name as a variable globally-scoped outside the for-loop, when it is being manipulated outside the for-loop before and after the execution of the for-loop.

Neither.

I am advocating introducing block-scope to while-loops, for-loops and if-elif-else statements.

I am advocating introducing block-scope to while-loops, for-loops and if-elif-else statements.

This would break all the code out there that relies on the existing scope rules.

1 Like

The program as exhibited is valid Python, so there is no way for the compiler to know that’s not what you meant.

In order to make inner a a different variable, would I have to declare it, for example like this?

a = 0
a += 1
print(a)

for i in range(3):
    local a
    if i == 0:
        a = 0
    else:
        a += i
    print(a)

a += 2
print(a)

No, the Python interpreter or compiler would need to be changed to allow block scoping of while-loops, for-loops or if-elif-else statements.

1 Like

Issuing new software releases of all programming languages and software tools is standard industry practice.

Release notes normally come with the release for developers past, present and future to consult on the impact of their software developments past, present and future.

It would be the responsibility of developers, past, present and future to write code which is compliant with their chosen release version of compiler or interpreter.

You will find it is rare that a language release changes the meaning of an unknowable amount of existing code. It creates a barrier to adoption of the other features added in that and later releases.

Python hasn’t made that kind of change for a long time. (I’m thinking of 2.x to 3.x). The reasons were compelling, the cost and timescale was huge, and it was done very carefully.

You’re entitled to think Python would be a better language if it had block scope, but I feel confident it isn’t going to acquire them.

1 Like

Clearly this is self-evident to you, but not to any of us. I’m trying to comprehend your example code but it just isn’t clear what the “right” semantics are here; in fact, my best guess would be that you’re expecting this to actually behave like a static variable in C, but that isn’t at all what you’re describing in words. So, no, I cannot see the problem. When you start with an attitude of “why on earth does nobody else see the gigantic problem that I see”, and then fail to explain yourself, you don’t exactly make a good case for the change.

Start over. Try to explain what the problem actually is. Don’t assume that we are listening in on your thoughts [1] and tell us what your proposal is.


  1. I mean, for ethical reasons we refuse to admit that we’re able to do that ↩

It will change the execution results of existing Python code, not the meaning.

Are you a Python programmer?

@Rosuav certainly is that!

2 Likes

Given that there’s no syntactical distinction between creating a variable and overwriting one in Python, how would you get information out of the block if that’s what you wanted?

biggest = items[0]
for item in items[1:]:
    if item > biggest:
        biggest = item  # <--- no op. This `biggest` is isolated
print(biggest)  # <--- This would unconditionally still be items[0]
1 Like

What does “no op.” mean?

What do you mean by “
isolated”?

Usually, in a task like this, that for-loop is inside a function definition for a function named something like find_maximum(numbers) which can then return the contents of that variable which in that case would be a locally-scoped variable inside the function definition to keep the variable from being accessed outside the function definition.

It can’t have the same meaning and produce a different answer. However, the statement is just as true with either word. It is rare that a language release changes the result when run of an unknowable amount of existing code.