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
?
pdb is a fully featured python shell, so you can execute arbitrary commands. locals() and globals() will display all the variables in scope with their values. You can use dir() if you're not interested in the values.
pdb. set_trace(*, header=None) Enter the debugger at the calling stack frame. This is useful to hard-code a breakpoint at a given point in a program, even if the code is not otherwise being debugged (e.g. when an assertion fails). If given, header is printed to the console just before debugging begins.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With