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

Stephen J. Turnbull turnbull.stephen.fw at u.tsukuba.ac.jp
Mon May 20 21:56:18 EDT 2019


Chris Angelico writes:
 > 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.

This has been discussed in the past, in the context of constructs like
comprehensions.  The sense was that given Python's declaration-less
variables, it's usually better to keep the scope hierarchy relatively
flat.  (The use of the word "sense" rather than "conclusion" is
deliberate.)  Comprehensions were judged to be an exception, and the
dummy variables in them are now confined to the scope of the
comprehension.

I suspect that many Pythonistas would argue that while in the general
context sometimes limiting the scope of a name is useful, equally
often it's a symptom of a function that has grown too big, and does
too many things.



More information about the Python-ideas mailing list