[issue41216] eval don't load local variable in dict and list comprehensions.

Andrej Klychin report at bugs.python.org
Sun Jul 5 20:31:03 EDT 2020


New submission from Andrej Klychin <andy_kl at bk.ru>:

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

----------
components: Interpreter Core
messages: 373054
nosy: Andy_kl
priority: normal
severity: normal
status: open
title: eval don't load local variable in dict and list comprehensions.
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41216>
_______________________________________


More information about the Python-bugs-list mailing list