classification
Title: eval don't load local variable in dict and list comprehensions.
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.9
process
Status: closed Resolution: duplicate
Dependencies: Superseder: eval() function in List Comprehension doesn't work
View: 5242
Assigned To: Nosy List: Andy_kl, eric.smith, xtreak
Priority: normal Keywords:

Created on 2020-07-06 00:31 by Andy_kl, last changed 2020-07-06 12:51 by eric.smith. This issue is now closed.

Messages (3)
msg373054 - (view) Author: Andrej Klychin (Andy_kl) Date: 2020-07-06 00:31
I'm not sure is it a bug or a fecature of comprehensions or eval, but intuitively it seems like it should work.

def foo(baz):
    return eval("[baz for _ in range(10)]")

foo(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in foo
  File "<string>", line 1, in <module>
  File "<string>", line 1, in <listcomp>
NameError: name 'baz' is not defined

def bar(baz):
    return eval("{i: baz for i in range(10)}")

bar(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in bar
  File "<string>", line 1, in <module>
  File "<string>", line 1, in <dictcomp>
NameError: name 'baz' is not defined
msg373069 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-07-06 05:31
This seems to be a duplicate of issue5242, issue36300
msg373114 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-07-06 12:51
Agreed that this is a duplicate, so I'm closing it.
History
Date User Action Args
2020-07-06 12:51:13eric.smithsetstatus: open -> closed

superseder: eval() function in List Comprehension doesn't work

nosy: + eric.smith
messages: + msg373114
resolution: duplicate
stage: resolved
2020-07-06 05:31:14xtreaksetnosy: + xtreak
messages: + msg373069
2020-07-06 00:31:03Andy_klcreate