[Python-ideas] Proposal: Allowing any variable to be used in a 'with... as...' expression

Terry Reedy tjreedy at udel.edu
Sun May 19 15:37:26 EDT 2019


On 5/19/2019 3:00 PM, Chris Angelico wrote:
> On Mon, May 20, 2019 at 4:46 AM Terry Reedy <tjreedy at udel.edu> wrote:
>>
>> 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)
> 
> Every other Python construct that represents a "grouping" of code is
> delimited with indentation. Why should variables be delimited with
> "del"? 

Indents are a precious resource.  To me, it is a plus that del does not 
use one


-- 
Terry Jan Reedy



More information about the Python-ideas mailing list