This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: 'eval' in generator expression behave different in dict from list
Type: behavior Stage: resolved
Components: Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Windson Yang, josh.r, peter.otten, rhettinger, yesheng
Priority: normal Keywords:

Created on 2018-11-19 06:24 by yesheng, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg330073 - (view) Author: (yesheng) Date: 2018-11-19 06:24
def yyy():
    a, b = 'abc', 'abd'
    print([eval(i) for i in ('a', 'b')])


def zzz():
    a, b = 'abc', 'abd'
    print({i: eval(i) for i in ('a', 'b')})


yyy()  # ok
zzz()  # NameError: name 'a' is not defined, however in yyy() it is ok
msg330074 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-11-19 07:18
I get NameError for both versions in both 3.6.5 and 3.7.1.
msg330075 - (view) Author: (yesheng) Date: 2018-11-19 07:42
I tried again, and could not reproduce on 3.6.7, latest update:

For 3.6.7: Both yyy() and zzz() got NameError

For 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)]
yyy() got ['abc', 'abd']
zzz() got NameError

2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:40:30) [MSC v.1500 64 bit (AMD64)]
yyy() got ['abc', 'abd']
zzz() got NameError
msg330077 - (view) Author: Peter Otten (peter.otten) * Date: 2018-11-19 07:49
You probably saw this is in Python 2.7 where it is the expected behaviour.
All versions of Python 3 should produce the NameError.
msg330121 - (view) Author: Windson Yang (Windson Yang) * Date: 2018-11-20 00:51
I get NameError for both versions in both 3.4.4, 3.5.2, 3.6.4, 3.7.1
msg330167 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2018-11-21 00:53
The "bug" is the expected behavior for 2.7, as previously noted, and does not exist on Python 3 (where list comprehensions follow the same rules as generator expressions for scoping), where NameErrors are raised consistently.
History
Date User Action Args
2022-04-11 14:59:08adminsetgithub: 79454
2018-11-21 08:46:22rhettingersetstatus: open -> closed
stage: resolved
2018-11-21 00:53:53josh.rsetversions: + Python 2.7, - Python 3.6
nosy: + josh.r

messages: + msg330167

resolution: not a bug
2018-11-20 00:51:49Windson Yangsetnosy: + Windson Yang
messages: + msg330121
2018-11-19 07:49:10peter.ottensetnosy: + peter.otten

messages: + msg330077
versions: + Python 3.6, - Python 2.7
2018-11-19 07:42:57yeshengsetmessages: + msg330075
versions: + Python 2.7, - Python 3.6
2018-11-19 07:18:31rhettingersetnosy: + rhettinger
messages: + msg330074
2018-11-19 06:24:18yeshengcreate