1

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.

S4M
  • 4,561
  • 6
  • 37
  • 47
  • Like putting the breakpoint immediately after the condition? – Moses Koledoye Sep 03 '16 at 19:48
  • @MosesKoledoye yes, I want to monitor `my_list` through the sub function(s) it is passed to. – S4M Sep 03 '16 at 19:49
  • 1
    Does this answer your question? http://stackoverflow.com/questions/6190468/how-to-trigger-function-on-value-change – Tom Fuller Sep 03 '16 at 21:16
  • @TomFuller it should, I can wrap up `my_list` inside an object, and make the functions to do some modifications to it trigger a break point - under certain conditions. If you want to expand you can put an answer and I will validate it. Thanks a lot, I wouldn't have thought of using a Design Pattern for that. – S4M Sep 03 '16 at 21:31

0 Answers0