0

I've come across an unusual bug/feature of pdb while I was testing a piece of code that sorts a list and returns the sorted indices.

mylist = [280, 275]
sorted_inds = sorted(range(len(mylist)), key=lambda i: mylist[i])

This works fine in regular python, returning [1, 0].

But when I try it in pdb I get a NameError:

*** NameError: name 'mylist' is not defined

This seems to be because the lambda function can't see/doesn't have access to the previously defined list mylist.

I'm looking for further insight into why this is the case. Why does it work in normal Python, but not in the debugger? What is different about the way variables are treated in the debugger?

berkelem
  • 2,005
  • 3
  • 18
  • 36

1 Answers1

0

After digging a bit I found this bug report from this answer from 2014. Originally, it was decided not to fix this, but subsequently a patch was developed that does fix it. It appears that this patch has not been implemented yet.

There is a suggested workaround in the bug report.

berkelem
  • 2,005
  • 3
  • 18
  • 36