Variable not recognized in nested or filtered list comprehension under IPython.embed()
See original GitHub issueIf I run IPython.embed()
and then try to execute the following code from the command line, I get a NameError
. Variables defined in the local namespace seem to become invisible when there is a nested list comprehension. They are visible for a single-level list comprehension, but I get a NameError if I refer to a variable from a second or deeper level.
This does not happen with a normal IPython shell or when I use IPython.start_ipython()
.
Here is the code to reproduce the error.
In [1]: x = [1, 2, 3]
In [2]: [i for i in x]
Out[2]: [1, 2, 3] # fine
In [3]: [i for i in x for j in x]
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-ebe839e86c71> in <module>
----> 1 [i for i in x for j in x]
<ipython-input-2-ebe839e86c71> in <listcomp>(.0)
----> 1 [i for i in x for j in x]
NameError: name 'x' is not defined
In [4]: y = [4, 5, 6]
In [5]: [i for i in x for j in y]
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-5-ba9a21c5a38c> in <module>
----> 1 [i for i in x for j in y]
<ipython-input-5-ba9a21c5a38c> in <listcomp>(.0)
----> 1 [i for i in x for j in y]
NameError: name 'y' is not defined
This is in IPython 7.9.0 with Python 3.7.3.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:9 (2 by maintainers)
Top Results From Across the Web
nested loop list comprehension in python - Stack Overflow
nested loop list comprehension in python ; can't recognize variable in outer ... The error shows NameError: global name 'a' is not defined....
Read more >IO tools (text, CSV, HDF5, …) — pandas 1.5.2 documentation
If a column or index contains an unparsable date, the entire column or index will be returned unaltered as an object data type....
Read more >self' not defined when using a list comprehension
when I run it I get 'NameError: name 'self' is not defined' on the last line. I printed the locals() before and during...
Read more >List Comprehension in Python — A Helpful Illustrated Guide
List comprehension is a compact way of creating lists. ... Use any variable in your expression that you have defined in the context...
Read more >TypeError: list indices must be integers or slices, not str
Usually, the most straightforward method for solving this problem is to convert any relevant values to integer type using the int() function. Something...
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
Same, I love IPython, I love IPython.embed(), and this issue makes it significantly less practical to work with 😢 .
I am facing this problems while debugging my code. Any updates?