I am trying to view the result of a list comprehension call using Python's debugging module, pdb. However the pdb "environment" is simultaneously claiming that a variable is and is not defined, resulting in a NameError
for a variable which pdb agrees is defined. The following is a minimal code example to duplicate the issue:
import pdb
def main():
bar = [0, 0, 1, 1]
foo(bar)
def foo(bar):
pdb.set_trace()
### pdb COMMANDS LISTED BELOW ARE CALLED HERE ###
print([False if bar[i] == 0 else True for i in range(len(bar))])
main()
Running the following pdb commands at the point of code execution indicated above leads to the following results.
(Pdb) p bar
[0, 0, 1, 1]
(Pdb) p [False if bar[i] == 0 else True for i in range(len(bar))]
*** NameError: name 'bar' is not defined
(Pdb) !print([False if bar[i] == 0 else True for i in range(len(bar))])
*** NameError: name 'bar' is not defined
(Pdb) n
[False, False, True, True]
Furthermore, running the code without the pdb module yields the expected result. Changing the location of the pdb.set_trace()
method call to the main
function has no impact on the result. What do I need to do in order to debug this list comprehension call?