Loading video player…

Printing Expressions

You can use the p command to print a variable’s value or the result of an expression. l will list 11 lines of code around our current line. If you continue entering that command, then you’ll see the next 11 lines of code. To reset the command, use l .

ll will show you just the local stack frame, usually the current function you’re stopped in. You’ll see more about stack frames in video 8 of this course.

Pressing Enter in a pdb prompt is the equivalent of entering the previously entered command. pp is short for pretty-print and will format the output of whatever we’re printing. This is especially useful for collections like dictionaries. q will quit out of the interactive debugger.

00:00 When using the print command p, you’re actually passing an expression to be evaluated by Python. If you pass a variable name, pdb will print its current value. However, you can do much more to investigate the state of your running application.

00:17 To demonstrate this, I created a new file called example2.py. In here, I have a function called get_path(), which takes a filename and returns the relative path to that file, not including the filename itself.

00:36 I have this Python script and a folder on my desktop called debugging/, and so running this program from within that directory will just return a dot ('.')—aka the current directory. I’m calling set_trace(), which will set a breakpoint on line 10.

00:54 That’s going to allow us to inspect our head and tail variables before this get_path() function returns. Just like before, I will run the script in my Z shell, and now we see that we have a breakpoint on line 10 within our function get_path().

01:13 Before this function returns, I want to get information about its local variables. But say I don’t remember what those are called? I can use the l command to list 11 lines of code around the current line, which is the breakpoint. So I’ll press Enter, and because our program is pretty short, we reach the end of the file at the bottom.

01:36 That’s where it says [EOF]. I’ll run l again, and we still see [EOF] (end of file). What’s happening here is the l command is trying to list out the next 11 lines in our program, but because there are no more lines, it’s just showing [EOF]. If I just hit Enter and supply no command, pdb will use the last command entered, which is still l. That is super important, so I’m going to say it one more time.

02:06 If I just press Enter at any point in our debugging session, pdb will enter the last command we punched in.

02:16 I’m going to do that a lot, so if you ever see me just press Enter, know that I’m just repeating the last command I actually entered. To reset the l command back to the top of our file, we can say l . and now we’re back at the top.

02:35 But this is a bit too much information.

02:39 If we want to see just the local stack frame, which in this case is the get_path() function we’re in, we can use the ll command, short for longlist.

02:50 Now we can see just the function we are currently in. I want to see what both the head and tail variables are, so I’ll type p head,tail.

03:04 Now, we see that head is a '.' and tail is the filename.

03:11 We can use the p command to print any valid Python expression. For example, we can call the getattr() function, passing in the get_path and '__doc__'.

03:26 You see we get the docstring for the get_path() function. If you’re printing something with a lot of output, like a large dictionary or a list, you can replace p with pp, short for pretty print, and that will give you a nicely-formatted output.

Avatar image for Pygator

Pygator on Sept. 22, 2019

wouldn’t ll make more since as local list instead of long list in this context?

Avatar image for Austin Cepalia

Austin Cepalia RP Team on Sept. 22, 2019

It would, but according to the documentation it stands for long list. That confused me too when I was researching for this course

Become a Member to join the conversation.