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: Anomaly of eval() of list comprehension
Type: behavior Stage: resolved
Components: 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: david2, mark.dickinson
Priority: normal Keywords:

Created on 2021-11-22 07:29 by david2, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg406743 - (view) Author: David Pratten (david2) Date: 2021-11-22 07:29
Hi

Example "eg def2" works but "eg def4" gives an error?

David

```
emp = [
    {
        "empno": 7839,
        "mgr": 0,
        "ename": "KING"
    },
    {
        "empno": 7566,
        "mgr": 7839,
        "ename": "JONES"
    },
    {
        "empno": 7698,
        "mgr": 7839,
        "ename": "BLAKE"
    }
]

a = [e for e in emp if e["mgr"] == 0]
print('eg 1', [b for b in a])
print('eg 2', eval('[b for b in a]'))
print('eg 3', [e for e in emp for b in a if e["mgr"] == b["empno"]])
print('eg 4', eval('[e for e in emp for b in a if e["mgr"] == b["empno"]]'))


def eval_anomaly():
    a_anomaly = [e for e in emp if e["mgr"] == 0]
    print('eg def1', [b for b in a_anomaly])
    print('eg def2', eval('[b for b in a_anomaly]'))
    print('eg def3', [e for e in emp for b in a_anomaly if e["mgr"] == b["empno"]])
    print('eg def4', eval('[e for e in emp for b in a_anomaly if e["mgr"] == b["empno"]]'))

eval_anomaly()
```
msg406748 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-11-22 08:35
Thanks for the report. The behaviour is by design: see #5242 (especially msg81898) for an explanation.

Closing this issue as a duplicate of #5242.
msg406749 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-11-22 08:37
See also #41216
msg406750 - (view) Author: David Pratten (david2) Date: 2021-11-22 08:43
Hi Mark,

Thanks.  

The anomaly is that the print("eg def2", ...)  works.  Should it not fail in the same way that print("eg def4", ...) does.

David

On 22/11/2021 7:36:31 PM, Mark Dickinson <report@bugs.python.org> wrote:

Mark Dickinson added the comment:

Thanks for the report. The behaviour is by design: see #5242 (especially msg81898) for an explanation.

Closing this issue as a duplicate of #5242.

----------
nosy: +mark.dickinson
resolution: -> duplicate
stage: -> resolved
status: open -> closed
superseder: -> eval() function in List Comprehension doesn't work

_______________________________________
Python tracker

_______________________________________

[755e9508-5fde-465a-ac8e-d82585c103f3]
msg406752 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-11-22 08:48
True: there's another detail here that's needed to explain the behaviour. The first "for" clause in a list comprehension is special: it's evaluated in the enclosing scope, rather than in the local function scope that the list comprehension creates. See the docs here: https://docs.python.org/3.9/reference/expressions.html?highlight=list%20comprehension#displays-for-lists-sets-and-dictionaries
History
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 90020
2021-11-22 08:48:17mark.dickinsonsetmessages: + msg406752
2021-11-22 08:43:33david2setmessages: + msg406750
2021-11-22 08:37:45mark.dickinsonsetmessages: + msg406749
2021-11-22 08:35:59mark.dickinsonsetstatus: open -> closed

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

nosy: + mark.dickinson
messages: + msg406748
resolution: duplicate
stage: resolved
2021-11-22 07:29:08david2create