question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

List comprehension with multiple "for" loops results in NameError

See original GitHub issue

Steps to reproduce:

import asteval

single_loop = """
[a for a in range(10)]
"""

double_loop = """
[(a,b) for a in range(10) for b in range(10)]
"""
aeval = asteval.Interpreter(use_numpy=False)

print(aeval.eval(single_loop))
print(aeval.eval(double_loop))

Results in the following output:

$ python3 ~/temp/astevaltest.py
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
NameError

[(a,b) for a in range(10) for b in range(10)]

        ^^^
name 'b' is not defined

This is not the same as a conventional python eval, which will evaluate the double_loop string as you would expect:

>>> eval("[(a,b) for a in range(10) for b in range(10)]")
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9)]
>>>

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
newvillecommented, Jul 18, 2022

@pwestlak-xilinx @sethw-xilinx I think it is actually the assignment aspect that is messed up. Hopefully, I’ll have time to look at this in detail this week.

0reactions
newvillecommented, Sep 7, 2022

@sethw-xilinx Yes, thanks very, very much!

Read more comments on GitHub >

github_iconTop Results From Across the Web

NameError in Python Nested for loops of List Comprehension
outer loop uses i before it is defined, so NameError occurred, i.e. your belief "the right most for is the outer one" is...
Read more >
How (Not?) to Use Python's List Comprehensions
You can think of a list comprehension as expanding to an accumulator-pattern nested for loop. Then the reason for the NameError is clear....
Read more >
NameError in Python Nested for loops of List Comprehension
I just want to know what is really happening here. score:4. Accepted answer. Correct, it's evaluated from left to right. To add the ......
Read more >
Comprehensions - The Conservative Python 3 Porting Guide
In Python 2, the iteration variable(s) of list comprehensions were considered local to ... as running the code under Python 3 will result...
Read more >
Understanding nested list comprehension syntax in Python
But according to your explanation, the first example should have a NameError, as x is not defined in the outer loop... Kurdo Bakur...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found