[issue43605] Eval/exec and comprehension scopes unclear in documentation

2021-03-27 Thread Cong Ma
Change by Cong Ma : -- keywords: +patch pull_requests: +23787 stage: needs patch -> patch review pull_request: https://github.com/python/cpython/pull/25039 ___ Python tracker _

[issue43605] Eval/exec and comprehension scopes unclear in documentation

2021-03-27 Thread Cong Ma
Cong Ma added the comment: Some more context: Issue 37646. The demo in that one was "eval inside list-comprehension-scope", while this one is the other way around. Perhaps another example may better illustrate the interplay between eval and the execution environment: ``` def f(): x = 1

[issue43605] Eval/exec and comprehension scopes unclear in documentation

2021-03-27 Thread Cong Ma
Cong Ma added the comment: > sum(get(i) for i in range(len(l))) This expression inside the body of ``func()`` references the name "get" and "l" (ell), both are local to the scope introduced by ``func()``. More specifically, these two names are referenced in the unnamed inner scope introduced

[issue43605] Eval/exec and comprehension scopes unclear in documentation

2021-03-27 Thread Bruno Loff
Bruno Loff added the comment: Hmm yes, some more words in the documentation might help. Does anyone understand why it happens, though? Specifically, note that sum(get(i) for i in range(len(l))) or eval("get(0) + get(1) + get(2) + get(3)") or eval("sum(get(i) for i in range(len(l)))", loc

[issue43605] Eval/exec and comprehension scopes unclear in documentation

2021-03-27 Thread Cong Ma
Cong Ma added the comment: I'm preparing an update to the documentation of eval/exec. There are several issues, but chiefly I'll link to the appropriate sections in the Language Reference, and clean up some other inaccuracies. When it's ready I'll submit a PR for core devs to review. --

[issue43605] Eval/exec and comprehension scopes unclear in documentation

2021-03-27 Thread Cong Ma
Cong Ma added the comment: > I think it is *very* reasonable to expect that calling eval() on a string > should have the exact same effect as if the code that is inside the eval had > been written as part of the source code. I don't think Python's execution model is defined this way. The doc

[issue43605] Eval/exec and comprehension scopes unclear in documentation

2021-03-26 Thread Bruno Loff
Bruno Loff added the comment: Hmm... OK, if I understand correctly, the evaluation procedure for a (e.g.) list comprehension, as described in the documentation you linked in, is as follows: * The generator in the leftmost for expression is evaluated in the current local scope. (doc: "The it

[issue43605] Eval/exec and comprehension scopes unclear in documentation

2021-03-26 Thread Terry J. Reedy
Terry J. Reedy 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