[Python-Dev] PEP 572, VF/B, and "Shark Jumping"
Ivan Pozdeev
vano at mail.mipt.ru
Thu Jul 5 13:52:24 EDT 2018
On 05.07.2018 3:22, Chris Angelico wrote:
> Python uses "as NAME" for things that
> are quite different from this, so it's confusing
I wrote in
https://mail.python.org/pipermail/python-dev/2018-June/154066.html that
this is easily refutable.
Looks like not for everybody. Okay, here goes:
The constructs that currently use `as' are:
* import module as m
* except Exception as e:
* with expr as obj:
* In `with', there's no need to assign both `expr' and its __enter__()
result -- because the whole idea of `with' is to put the object through
`__enter__', and because a sane `__enter__()' implementation will return
`self' anyway (or something with the same semantic -- i.e. _effectively_
`self'). But just in case, the double-assignment can be written as:
with (expr as obj) as ctx_obj:
by giving "as" lower priority than `with'. As I said, the need for this
is nigh-nonexistent.
* `import' doesn't allow expressions (so no syntactic clash here), but
the semantic of "as" here is equivalent to the AE, so no confusion here.
* Same goes for `except`: doesn't accept expressions, same semantic.
So, with "as" only `with' becomes the exception -- and an easily
explainable one since its whole purpose is to implicitly call the
context manager interface.
More information about the Python-Dev
mailing list