These examples in the PEP, they all seem bad to me, written to be too clever by someone who doesn't think well.
E.g.:
filtered_data = [y for x in data if (y := f(x)) is not None]
How about: filtered_data = [y for y in (f(x) for x in data) if y is not None]
Or just? filtered_data = filter(None, map(f, data))
(If f() can return non-None "Falsey" values then this would require a predicate function. But maybe f() is badly designed?)Or this:
if any(len(longline := line) >= 100 for line in lines):
print("Extremely long line:", longline)
What's wrong with the "old-fashioned" way? for line in lines:
if len(line) >= 100:
print("Extremely long line:", line)
break
Of course, in the bad old days when loop variables leaked I think you could just write: if any(len(line) >= 100 for line in lines):
print("Extremely long line:", line)
But I'm not sure, and in any event leaking loop vars was fixed at some point. (Good. It was useful but bug-prone. Just like this PEP!)To me this is also a mess:
results = [(x, y, x/y) for x in input_data if (y := f(x)) > 0]
It should be: results = [(x, y, x / y) for x, y in zip(input_data, map(f, input_data)) if y > 0]
Maybe all the examples are just poorly chosen, but I don't believe that. I think this PEP is just a bad idea. Badly bad.