Sorry for the terrible title. I had to figure out the terminology and an trying to put all relevant points there.
Consider the following simple interaction in Python:
Python 3.6.9 (default, Jul 21 2019, 14:33:59)
[GCC 7.4.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> python.el: native completion setup loaded
>>> s1 = [ i for i in range(10)]
>>> [i for i in range(len(s1)) if s1[i]%2 == 0]
[0, 2, 4, 6, 8]
The last statement creates (and prints) indexes of the even elements in the original array s1.
But the equivalent does not work in pdb:
(Pdb) !s1 = [ i for i in range(10)]
(Pdb) s1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
(Pdb) ![i for i in range(len(s1)) if s1[i]%2 == 0]
*** NameError: name 's1' is not defined
Why is s1 in scope in python interpreter but not in debugger? I am trying to identify elements in the array that meet certain criteria. What is the python way to do this in debugger?
Like others have mentioned, your code should work. However, I ran into a similar issue some time ago when I tried to use a variable in a list comprehension like you and I found Antimony's response here very useful:
"In Python 3, you have to use the interact command in pdb before you can access any non-global variables due to a change in the way comprehensions are implemented."
List comprehension scope error from Python debugger
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With