On 27Nov2020 00:25, Steven D'Aprano
Block scoping allows shadowing within a function.
Just to this: it needn't.
You could forbid shadowing of the _static_ outer scope easily enough at
parse/compile time. That would prevent a certain class of easy misuse.
i = 9
{ new scope here for "i" ==> parse/compile error, since "i" is in
play
}
It depends what the scope's for: to define a variable lifetime (very
desireable sometimes) or to _actually_ shadow something (I'm against
that myself - there are better and less error prone ways).
Anyway, I'm against proliferation of new scopes on the whole.
That said, there _are_ times I wish I could mark out the lifetime of a
variable, akin to C level:
... i does not exist ...
{ int i;
... use i ...
}
... i now unknown, use is an error ...
The nearest Python equivalent is:
i = blah()
... use i
del i
which feels fragile - accidental assignment to "i" later is not
forbidden.
Of course, in C variables must be declared. Not so easy with Python,
syntacticly.
Cheers,
Cameron Simpson