8

I'm debugging a python script, and I want to watch a variable and get notified whenever its value changes.

Is there a way to do this in pudb?

Jeff Widman
  • 22,014
  • 12
  • 72
  • 88

1 Answers1

15

You can't simply ask for notification any time a value changes (that I'm aware of).

However, you can set both watch expressions and conditional breakpoints which should provide the capability that you're looking for.

First, go to the variable list (shift+V), then N to add a new watch. Enter in whatever variable you want to watch.

Now set a breakpoint at the places that your value can change - back to the main window , then find the lines and hit B. Then let your program run to that line or until your variable is defined.

Then shift+B to select the breakpoints window. Press enter to edit the breakpoint. Add a conditional expression - since your value should be set by now, you can see the value in your watch list. A simple <variable> != <current value> should do. Or you can enter a specific criteria.

Now back to the main window and let your program continue. When your conditional is true at that breakpoint, your program will stop and you will see the value in your watch list.

For an example, see the following screencast:

asciicast

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
  • Can I also watch a class attribute? e.g. `self.foo` or `MyClass.foo` – djangonaut Jan 18 '18 at 20:21
  • You should be able to watch *any* expression - so if it's code that can be evaluated, you should be able to watch it :) – Wayne Werner Jan 18 '18 at 20:26
  • I'm trying to stop when a class or one of the parent classes set an attribute for the first time, so it is not part of the variable list. So far no luck, still trying... – djangonaut Jan 18 '18 at 20:37