How to watch a variable in pudb?

2019-07-14 06:18发布

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?

标签: python pudb
1条回答
时光不老,我们不散
2楼-- · 2019-07-14 06:37

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

查看更多
登录 后发表回答