[Python-ideas] Proposal: Allowing any variable to be used in a 'with... as...' expression
Terry Reedy
tjreedy at udel.edu
Sun May 19 14:45:42 EDT 2019
On 5/18/2019 10:01 PM, Chris Angelico wrote:
> 2) Redefine the 'with' block or create a new syntactic form such that
> the variable actually creates a subscope. That way, at the end of the
> block, the name would revert to its former meaning.
>
> x = 1
> with local 2 as x:
> print(x) # 2
> print(x) # 1
Since Python allows both multiple character names and unicode names,
there is no shortage of names, and hence hardly any need for such
temporary rebinding. Function and class bodies must be sub-scoped each
for their own reasons.
> This would have definite value,
Not much. If one wants to explicitly localize the use of (new) names,
one can explicitly delete them.
x1 = 1
...
x2, x3 = 2, 3
h = hypot(x2, x3)
del x2, x3 # Okay reader, you can forget about x2, x3.
...
print(1)
--
Terry Jan Reedy
More information about the Python-ideas
mailing list