What about make the scope of a1 identical to a2. Both can not be used outside the control statement.
if a1 := 1
a2 = 0
print(a1) # maybe reports error too
print(a2) # error
But I guess, maybe it does not matter. In Python, in many cases I can not even distinguish variable definition from assignment. And it seems that there will never be name collision.
a = 'abc'
if a := 1:
print(a)
if a := True:
print(a)
Aye. The scopes of names are deliberately few in Python. IMO this is a
good thing:
builtins
module
functions
and that’s it!
While I am a fan of C’s:
for (int i=0; i<10; i++) {
... i defined only here ...
}
for trite things like loop counters, and also of C’s:
{ int i;
... i defined only here ...
}
subblocks, I’m generally against lots of scopes.
If you want a new scope, define a function. It can even be inside your
code as a closure if you need access to the outer scope.
In particular, scopes bring variable name shadowing, which can be a
confusing source of bugs sometimes. I’d be happier if some hypothetical
scope syntax fordade shadowing, but that is not normally the case.
(And it is problematic in a dynamic language like Python where names
only get defined when assigned to).