[Python-Dev] Informal educator feedback on PEP 572 (was Re: 2018 Python Language Summit coverage, last part)
> So what about:
> l = [x:=i for i in range(3)]
> g = (x:=i for i in range(3))
> Is there any way to keep these consistent if the "x" is in the regular
I'm not clear on what the question is. The list comprehension would bind `
l ` to [0, 1, 2] and leave the local `x` bound to 2. The second example
binds `g` to a generator object, which just sits there unexecuted. That
has nothing to do with the PEP, though.
If you go on to do, e.g.,
l = list(g)
then, same as the listcomp, `l` will be bound to [0, 1, 2] and the local
`x` will be left bound to 2.
The only real difference is in _when_ the `x:=i for i in range(3)` part
gets executed. There's no new twist here due to the PEP. Put a body B in
a listcomp and any side effects due to executing B happen right away, but
put B in a genexp and they don't happen until you force the genexp to yield
For example, do you think these two are "consistent" today?
l = [print(i) for i in range(3)]
g = (print(i) for i in range(3))
? If so, nothing essential changes by replacing "print(i)" with "x := i" -
in either case the side effects happen when the body is executed.
But if you don't think they're already consistent, then nothing gets less
consistent either ;-)
-------------- next part --------------
An HTML attachment was scrubbed...