Terry J. Reedy <tjre...@udel.edu> added the comment:

This is not an execution bug.

https://docs.python.org/3/reference/expressions.html#displays-for-lists-sets-and-dictionaries

"However, aside from the iterable expression in the leftmost for clause, the 
comprehension is executed in a separate implicitly nested scope. This ensures 
that names assigned to in the target list don’t “leak” into the enclosing 
scope."

So when the passed in locals is not the passed in globals, that means a 
separate local scope.

https://docs.python.org/3/reference/expressions.html#generator-expressions is a 
little less clear.

"Variables used in the generator expression are evaluated lazily when the 
__next__() method is called for the generator object (in the same fashion as 
normal generators). However, the iterable expression in the leftmost for clause 
is immediately evaluated, so that an error produced by it will be emitted at 
the point where the generator expression is defined, rather than at the point 
where the first value is retrieved. Subsequent for clauses and any filter 
condition in the leftmost for clause cannot be evaluated in the enclosing scope 
as they may depend on the values obtained from the leftmost iterable."

By implication, the value expressions are also not evaluated in the enclosing 
local scope.

I am thinking about adding something to the eval/exec doc, but this issue 
overlaps with another one about their doc.





https://docs.python.org/3/reference/expressions.html#generator-expressions

----------
assignee:  -> docs@python
components: +Documentation
nosy: +docs@python, terry.reedy
stage:  -> needs patch
title: Issue of scopes unclear in documentation, or wrongly implemented -> 
Eval/exec and comprehension scopes unclear in documentation
versions: +Python 3.10 -Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue43605>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to