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

Jacco van Dorp j.van.dorp at deonet.nl
Wed May 9 03:15:49 EDT 2018


> [Tim]
> {About binding the for loop variable}

Yeah, that binding is the one I attempted to refer to. So I did
understand you after all.

> We should probably define what happens when you write [p := p for p in
> range(10)]. I propose that this overwrites the loop control variable rather
> than creating a second p in the containing scope -- either way it's probably
> a typo anyway.

My naive assumption would be both. If it's just the insertion of a
nonlocal statement like Tim suggested, wouldn't the comprehension blow
up to:

def implicitfunc()
  nonlocal p
  templist = []
  for p in range(10):
    p = p
    templist.append(p)
  return templist

?

If it were [q := p for p in range(10)], it would be:

def implicitfunc()
  nonlocal q
  templist = []
  for p in range(10):
    q = p
    templist.append(q)
  return templist

Why would it need to be treated differently ?
(type checkers probably should, though.)

> [Tim]
> A compile-time error would be fine by me too.  Creating two meanings
> for `p` is nuts - pick one in case of conflict.  I suggested before
> that the first person with a real use case for this silliness should
> get the meaning their use case needs, but nobody bit, so "it's local
> then" is fine.

x = x is legal. Why wouldn't p := p be ?

> [Juancarlo]
> Maybe my distrust is just don't like the new syntax, or that I'am biased towards using "as".

I share your bias, but not your distrust. (go as !)


More information about the Python-ideas mailing list