Let's say I have the following code:
def f(lst,k):
if k % 5 == 0:
lst.append(str(k+1))
return k*len(lst)
def big_function():
my_list = ["a","b","c","d"]
s = 0
for k in range(10):
tmp = f(my_list,k)
s += tmp
return s
If I run it, the variable my_list
in the function big_function
will become ["a","b","c","d","6"]
. Let's say that the following was a mistake:
if k % 5 == 0:
lst.append(str(k+1))
And that I didn't intend to modify my_list
.
Is there a way to run pdb in a way that would be like python -m pdb my_script.py --condition "is_modified(my_list, scope = big_function)"
and so the program is interrupted with a debug prompt at the line lst.append(str(k+1))
when k
is 5.
The point is that while this particular example is very simple and straightforward to analyze, in a bigger function it will be hard to track when a variable got modified by a sub function, and I want pdb to do that work for me but so far I've been unsuccessful.