One of the examples given in PEP572 is
# Reuse a value that's expensive to compute
[y := f(x), y**2, y**3]
currently in python, you'd have to do one of the following:
# option 1
y = f(x)
[y, y**2, y**3]
or
# option 2
[f(x), f(x)**2, f(x)**3]
the example implies that option 2 here could be improved, but I have never seen that recommended over the first option. Is there ever a reason why option 2 (and therefore the walrus operator) would be better than option 1?
y = f(x); [y, y**2, y**3]
and[f(x), f(x)**2, f(x)**3]
are not doing the same thing, in general, and[y := f(x), y**2, y**3]
is only equivalent to the two-line code. – Stupe