On 03/24/2018 09:00 PM, Chris Angelico wrote:
On Sun, Mar 25, 2018 at 2:59 PM, Ethan Furman wrote:
On 03/24/2018 08:51 PM, Chris Angelico wrote:

Let's suppose we have assignment expressions. I'm going to use "(expr
as name)" syntax for this example.

a = [(1 as b) for c in (d as e) if (2 as f)]

Which of these six names is local to the comprehension and which can
leak? Due to the requirements of class scope, 'd' must be looked up in
the outer scope. That means that its corresponding 'as e' must also
land in the outer scope. [...]


Why?

d = 9
def func():
   e = d

d is looked up outside of func, but e is still local to func.

No, it's looked up inside func, and the value is found outside of
func. Consider:

d = 9
def func():
     e = d
     d = 1

That is invalid Python.

What's happening with a comprehension is more like:

d = 9
def func(_iter):
     ... body of comprehension
func((d as e))

which is looking it up _outside_ the function, then passing it as a parameter.

Looks like a buggy implementation detail. Any assignments that happen inside a listcomp should be effective only inside the listcomp.

--
~Ethan~
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to