Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to watch a variable in pudb?

Tags:

python

pudb

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?

like image 830
Jeff Widman Avatar asked Oct 19 '17 02:10

Jeff Widman


People also ask

How do I list all variables in pdb?

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.

What is pdb Set_trace ()?

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.


1 Answers

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

like image 144
Wayne Werner Avatar answered Sep 30 '22 17:09

Wayne Werner