List comprehension with multiple "for" loops results in NameError
See original GitHub issueSteps 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:
- Created a year ago
- Comments:6 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.
@sethw-xilinx Yes, thanks very, very much!