[New-bugs-announce] [issue23714] Comprehension + eval doesn't work inside function definition

Ariel Bruner report at bugs.python.org
Thu Mar 19 22:31:47 CET 2015


New submission from Ariel Bruner:

The following code gives a NameError:

>>> def foo(bar):
	print {eval(x) for x in ['bar']}


>>> foo(2)

Traceback (most recent call last):
  File "<pyshell#52>", line 1, in <module>
    foo(2)
  File "<pyshell#51>", line 2, in foo
    print {eval(x) for x in ['bar']}
  File "<pyshell#51>", line 2, in <setcomp>
    print {eval(x) for x in ['bar']}
  File "<string>", line 1, in <module>
NameError: name 'bar' is not defined

I've seen this kind of bug reported for Python 3.X (e.g. Issue5242), and the behavior seems to be identical (e.g. can be fixed with {eval(x) for x in ['bar'] if True or bar}), but the previously reported bug reports mention comprehension is implemented differently on 2.X and that it should not happen there, so I thought that might be of interest.

----------
components: Windows
messages: 238561
nosy: Ariel Bruner, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
status: open
title: Comprehension + eval doesn't work inside function definition
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23714>
_______________________________________


More information about the New-bugs-announce mailing list