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

Stepping Through Code

There are two commands you can use to step through code when debugging:

  1. n stands for next. It allows you to move to the next logically executed line of code, ignoring function calls. This is the equivalent of step over in most debuggers.

  2. s stands for step. If you’re stopped on a function call, move into that function and stop there. This is the equivalent of step into in most debuggers.

00:00 There are two commands we can use to step through code when debugging: n, short for next, and s, short for step.

00:11 If you’ve ever used a debugger before, you’re probably familiar with these already. n is equivalent to step over, and s is equivalent to step into.

00:24 I’ve got a new script here called example3, which is very similar to the last one, except I’m setting the breakpoint so execution stops on line 14.

00:36 I’m also storing the relative file path in a variable called filename_path before printing it. Just like before, I’ll run this program and we’ll hit the breakpoint at line 14.

00:51 Now I’ll type n, and you’ll see that we skipped over the function call to get_path() and have moved on to the print() line. From here, I can print the filename_path by saying p filename_path, and we get a dot ('.'), because the current directory is the same as where our file is located.

01:16 Now let’s try using s, or step into. I’ll use q to quit debugging,

01:24 I’ll clear my screen,

01:27 and now I will run this program once more. This time, I’ll type s, and you see we get a different output. First we see --Call--, which tells us that we’re entering a function call.

01:40 Then we’ve got information about where we are, line number 6, which defines the get_path() function. Now I can use n to continue stepping through the program normally, and I can hit Enter to repeat the previous n command.

01:58 Once we’re about to return from the function, we see --Return-- at the top of the output, and we can also see what the function will return—in this case, a '.'. That’s the little arrow (->) after get_path().

02:13 I’ll hit Enter two more times, and now we’re back at the module level. Our print() function was executed, and so we see path = . in the output.

02:25 pdb tells us that we’re about to return from the module level, which in this case will return the NoneType. I’ll press Enter one last time, and that’s it!

Avatar image for Pygator

Pygator on Sept. 22, 2019

Is there any good c++ equivalent for all of these features? Thank you!! This will be very handy.

Avatar image for Geir Arne Hjelle

Geir Arne Hjelle RP Team on Sept. 22, 2019

pdb is very much based on gdb, the GNU debugger: www.gnu.org/software/gdb/

gdb supports languages like C, C++, Fortran, etc and uses essentially the same commands as pdb.

Avatar image for Pygator

Pygator on Sept. 23, 2019

Thanks for the reference.

Avatar image for Sachin

Sachin on Dec. 14, 2019

For example3.py, on 13th line it seems we are setting up the pdb trace, not sure then why your video execution pointer is on print(f”path = {get_path(filename)}”). When I run the same command, I see the execution pointer is on filename_path = get_path(filename).

Code “”” import os def get_path(filename): “”“return file’s path of empty string if no path”“” head, tail = os.path.split(filename)

return head

filename = file import pdb; pdb.set_trace() filename_path = get_path(filename)

print(f”path = {get_path(filename)}”) “”” output:

/Users/skamble/GitHub/WALMART/example2.py(11)<module>() -> filename_path = get_path(filename) (Pdb) l 6 return head 7 8 9 filename = file 10 import pdb; pdb.set_trace() 11 -> filename_path = get_path(filename) 12 13 print(f”path = {get_path(filename)}”) [EOF]

Become a Member to join the conversation.