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

Alexander Belopolsky alexander.belopolsky at gmail.com
Fri May 4 21:56:10 EDT 2018

On Fri, May 4, 2018 at 8:06 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> ...
> With that spelling, the three examples above would become:
>     # Exactly one branch is executed here
>     if m given m = pattern.search(data):
>         ...
>     elif m given m = other_pattern.search(data)):
>         ...
>     else:
>         ...
>     # This name is rebound on each trip around the loop
>     while m given m = pattern.search(remaining_data):
>         ...
>     # "f(x)" is only evaluated once on each iteration
>     result = [(x, y, x/y) for x in data if y given y = f(x)]

I think this is a step in the right direction.  I stayed away from the
PEP 572 discussions because while intuitively it felt wrong, I could
not formulate what exactly was wrong with the assignment expressions
proposals.  This proposal has finally made me realize why I did not
like PEP 572.  The strong expression vs. statement dichotomy is one of
the key features that set Python apart from many other languages and
it makes Python programs much easier to understand.  Right from the
title, "Assignment Expressions", PEP 572 was set to destroy the very
feature that in my view is responsible for much of Python's success.

Unlike PEP 572, Nick's proposal does not feel like changing the syntax
of Python expressions, instead it feels like an extension to the if-,
while- and for-statements syntax.  (While comprehensions are
expressions, for the purposes of this proposal I am willing to view
them as for-statements in disguise.)

More information about the Python-ideas mailing list