Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Displaying Expressions

You can use display to display variables or expression outputs that change:

$ display char

This will display the value of the char variable each time it changes. If char doesn’t change on the next execution, then it will not be displayed.

The display command creates a watchlist. We can add more variables by running display again with a new variable name or expression. Running display without additional arguments will show us our entire watchlist. undisplay will clear the watchlist. If we pass in a variable name, then it will remove that variable from the watch list.

00:00 Earlier, we printed expressions and variables with p and pp, but typing that in over and over again gets pretty repetitive and so pdb provides us with a way to automatically display an expression or a variable’s value onscreen if it’s changed.

00:20 Let’s see how that works. I’m here in the same file as the last video, example4unt.py. I’m going to run this in my Z shell, and I’ll use ll to show the current breakpoint in the context of the function it’s in.

00:40 Let’s set a breakpoint on line 11 by typing b 11.

00:47 Now I’ll use the c command to continue execution up until this new breakpoint. If I type display char here, we’ll see the value 'e' display on the screen.

01:01 I’ll type c again to iterate one more time through this for loop, stopping at the breakpoint, and now we see it says the current value of char is 'x', and it used to be 'e'. That’s where it says [old: 'e']. It only showed us this information because the value of char changed.

01:23 If I keep running c, we’ll see the value of char through each iteration, because it changes every single time. There’s also another helpful way to use display.

01:35 If we supply it multiple expressions, we can create a watch list that will automatically display when any of our watch list expressions has changed. I’m going to quit out of pdb and I’ll clear my shell,

01:52 and now I’ll run this program once more. Once again, I’ll create a breakpoint at line 11, I will continue execution, and now I’m going to start listing variables I want in my watch list.

02:09 I want to display char. I want to display fname. I want to display head, and we’ll also display tail.

02:21 Now if I run the c command, you’ll see that char is showing because it changed. If any other variables in our watch list had changed through that iteration, their new values would show here as well.

02:36 I can say display to list the current state of all my variables. If I want to clear my watch list, I could write undisplay, and I could do something like undisplay char to remove only the char variable from my watch list.

02:54 If caveman could debug their Python programs with a watch list, this is probably how they would do it.

Become a Member to join the conversation.