[Python-ideas] A PEP on introducing variables on 'if' and 'while'

Juancarlo Añez apalala at gmail.com
Sun Jun 10 08:08:13 EDT 2018

> The only difference is that the `except` block implicitly unbinds the
> variable when the block ends.

Mmmm. Good to know, because that means that the semantics are the same,

>     while (condition := expression) as flag:
>         ...

Ah! Are the parenthesis necessary there?

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.

Most of the arguments in favor of ':=' have been through examples of
generators in which the introduced name is used within, with the if/while
case often forgotten.

There can be cases in which combining both syntaxes is useful:

x = None
while compute(x := v for v in next_series_value(x)) as comp:
    x = comp

> Like it or not, I expect that they will be seen as competing PEPs, not
> independent ones.

Finding a real-world example of something like the above synthetic example
would be in favor of the orthogonality.

Juancarlo *Añez*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180610/08d58816/attachment.html>

More information about the Python-ideas mailing list