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.
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.
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.
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.
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.
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.
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!
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]
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.