[Python-ideas] Inline assignments using "given" clauses

Chris Angelico rosuav at gmail.com
Sat May 12 18:55:07 EDT 2018

On Sun, May 13, 2018 at 8:47 AM, Juancarlo Añez <apalala at gmail.com> wrote:
>> My main point here is that "with" works as well as "given" in this form
>> from an English prose point of view.
> +1 for "with...as", -1 for ":="
> About affecting existing contexts, it seems that "with..as" would create a
> new context just for the expression, and the control statement it is
> embedded in, similar to what the current "with" statement does. These are
> semantics that are really easy to explain.

The trouble with every variant involving 'with' is that the semantics
LOOK similar, but are subtly different. The current 'with' statement
doesn't create a subscope; the only "context" it creates is regarding
the resource represented by the context manager. For instance, opening
a file in a 'with' block will close the file at the end of the block -
but you still have a (closed) file object. Using "with... as" for name
bindings wouldn't call __enter__ or __exit__, so it won't create that
kind of context; and whether it creates a subscope for the variable or
not, it's not going to match the 'with' statement.


More information about the Python-ideas mailing list