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

Victor Stinner vstinner at redhat.com
Wed Apr 25 19:05:42 EDT 2018


>     # 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]

Victor


More information about the Python-Dev mailing list