[Python-Dev] The new and improved PEP 572, same great taste with 75% less complexity!

Chris Angelico rosuav at gmail.com
Wed Apr 25 19:42:58 EDT 2018


On Thu, Apr 26, 2018 at 9:05 AM, Victor Stinner <vstinner at redhat.com> wrote:
>>     # Handle a matched regex
>>     if (match := pattern.search(data)) is not None:
>>         ...
>>
>>     # A more explicit alternative to the 2-arg form of iter() invocation
>>     while (value := read_next_item()) is not None:
>>         ...
>>
>>     # Share a subexpression between a comprehension filter clause and its output
>>     filtered_data = [y for x in data if (y := f(x)) is not None]
>
> What do you think of adding the variant without the new ":=" syntax?
> It would help to see the benefit of the new ":=" syntax, and help to
> compare the two syntaxes.
>
> if:
>   if (match := pattern.search(data)) is not None: ...
> vs
>   match = pattern.search(data)
>   if match is not None: ...
>
> while:
>   while (value := read_next_item()) is not None: ...
> vs
>   while True:
>     value = read_next_item()
>     if value is None: break
>     ...
>
> list-comprehension:
>   filtered_data = [y for x in data if (y := f(x)) is not None]
> vs
>   filtered_data = [f(x) for x in data]
>   filtered_data = [x for x in filtered_data if x is not None]

Doing that for everything would put the PEP solidly into "TL;DR"
territory, I'm afraid. There's already too much verbiage in some of
those sections.

Plus, we'd get right back into debates about how if you just change
your intended semantics slightly, there's a completely different way
to achieve something actually quite different, and we've already been
around that a few times already.

ChrisA


More information about the Python-Dev mailing list