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?