[Python-ideas] A PEP on introducing variables on 'if' and 'while'
Steven D'Aprano
steve at pearwood.info
Sat Jun 9 19:50:30 EDT 2018
On Sat, Jun 09, 2018 at 09:17:37AM -0400, Juancarlo Añez wrote:
> > Do you mean the context manager semantics of with statements? As in,
> > calling the __enter__ and __exit__ method?
> >
>
> No. Just the scope of the variables introduced, which is different in `with
> as` and `except as`.
They aren't. They are the same scope: both `with` and `except` bind to a
local variable.
The only difference is that the `except` block implicitly unbinds the
variable when the block ends.
py> err = "something"
py> try:
... None + 1
... except TypeError as err:
... pass
...
py> err
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'err' is not defined
> > Please make sure you are very familiar with PEP 572 before you do, and
> > expect to have your PEP compared to it.
>
> My intention would be to make the to proposals orthogonal, if possible, so
> both/any can be accepted or rejected in their own timeline.
>
> I'm certain that both can live together.
Seems redundant...
while (condition := expression) as flag:
...
Accepting "while/if as name" would remove much (but not all) of the
motivation for assignment expressions, while accepting assignment
expressions would make a dedicated while/if as name syntax unnecessary.
Like it or not, I expect that they will be seen as competing PEPs, not
independent ones.
--
Steve
More information about the Python-ideas
mailing list