[Python-Dev] PEP 572: Assignment Expressions

Paul G paul at ganssle.io
Mon Apr 23 18:36:18 EDT 2018


On 04/23/2018 06:04 PM, Tim Peters wrote:

> However, against "as" is that its current use in "with" statements
> does something quite different:
> 
>     with f() as name:
> 
> does not bind the result of `f()` to `name`, but the result of
> `f().__enter__()`.  Whether that "should be" fatal, I don't know, but
> it's at least annoying ;-)


This could be read a different way, though, since `with f()` calls `f().__enter__()`, you could read it as `(with f()) as name:`, in which case it does the same thing as an `as`-based binding expression would. Viewing it that way *also* helps alleviate the cognitive problem that `with f() as name` and `with (f() as name)` do two different things - the parentheses there are changing the precedence in the same way that `2 + 4 * 3` and `(2 + 4) * 3` do two different things.

This sorta also works for `except`, if you read it as `(except SomeException) as e:`, but I think this fiction falls apart when you look at `import`, since `import foo` *already* binds "foo" to a name, and `import foo as bar` not only binds `foo` to `bar`, but also *doesn't* bind `foo` to `foo`.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180423/781ce2c6/attachment.sig>


More information about the Python-Dev mailing list