[Python-ideas] A comprehension scope issue in PEP 572

Matt Arcidy marcidy at gmail.com
Sun May 6 22:37:27 EDT 2018

> Personally, I'd still like to go back to := creating a statement-local
> name, one that won't leak out of ANY statement. But the tide was
> against that one, so I gave up on it.


I have some probably tangential to bad arguments but I'm going to make
them anyways, because I think := makes the most sense along with SLNB.

first, := vs post-hoc (e.g. where or given)

base case:
[ x for x in range(1) ]
while obvious to all of us, reading left to right does not yield what
x is till later.
[ (x, y) for x in range(1) for y in range(1) ] doubly so.
If x or y were defined above, it would not be clear until the right
end if what contex they had.

[ (x, y) for x in range(n) given y = f(n) ]
i dont know what's the iterator till  after 'for'

[ (x, y:=f(n) for x in range(n) ]
At a minimum, I learn immediately that y is not the iterator.
Slightly less cognitive load.

it's not that one is better, or that either is unfamiliar, it's about
having to hold a "promise" in my working memory, vs getting an
immediate assignment earlier.  (it's a metric!)

now my silly argument.
":" is like a "when" operator.

if y==x:

> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/

More information about the Python-ideas mailing list