1 00:00:00,120 --> 00:00:03,620 One of the major challenges we all go through as programmers is learning to 2 00:00:03,620 --> 00:00:06,640 track the state of variables at any given time. 3 00:00:06,640 --> 00:00:08,980 For example, if you're working on a list, pending and 4 00:00:08,980 --> 00:00:13,910 popping items, it's pretty easy to lose track of what index a certain value is at. 5 00:00:13,910 --> 00:00:17,100 Sure, you can print out the list, or use the logging library to track it, 6 00:00:17,100 --> 00:00:19,360 but what if there was a better way? 7 00:00:19,360 --> 00:00:21,660 What if there's a way to get inside your code? 8 00:00:21,660 --> 00:00:24,950 What if we could shrink ourselves down small enough to go inside the computer 9 00:00:24,950 --> 00:00:26,280 and wait. 10 00:00:26,280 --> 00:00:28,322 I'm pretty sure that's the plot of a bad sc-fi movie. 11 00:00:28,322 --> 00:00:30,210 Probably several of them. 12 00:00:30,210 --> 00:00:32,020 Okay, but the question's a good one. 13 00:00:32,020 --> 00:00:34,500 Not shrinking but seeing what's going on. 14 00:00:34,500 --> 00:00:36,980 What if we could get inside the script while it's running and 15 00:00:36,980 --> 00:00:38,780 see the state of everything? 16 00:00:38,780 --> 00:00:41,220 You know, I wouldn't bring this up if it wasn't possible. 17 00:00:41,220 --> 00:00:46,000 This is actually built into python and it's known as the Python Debugger or pdb. 18 00:00:46,000 --> 00:00:48,440 Let's go to workspaces and see how it works. 19 00:00:48,440 --> 00:00:51,450 All right. So I made this file called buggy.py and 20 00:00:51,450 --> 00:00:54,270 it has this list of items that's all jumbled up. 21 00:00:55,450 --> 00:00:57,610 We've had a list like this in a previous code challenge. 22 00:00:57,610 --> 00:00:59,210 Some of you probably remember that. 23 00:00:59,210 --> 00:01:03,750 Sometimes you get lists like this from an API or by capturing user input. 24 00:01:03,750 --> 00:01:06,350 I mean, it comes from various locations. 25 00:01:06,350 --> 00:01:11,180 So I want to write some code to modify this list. 26 00:01:11,180 --> 00:01:13,440 I want the whole list to be just numbers. 27 00:01:13,440 --> 00:01:15,020 I don't want anything that is not a number. 28 00:01:15,020 --> 00:01:18,450 And we're gonna pretend I don't know any other ways to do that. 29 00:01:18,450 --> 00:01:22,920 And that I don't know about the remove method on lists, okay? 30 00:01:22,920 --> 00:01:24,780 So yes, this is bad code. 31 00:01:24,780 --> 00:01:26,370 It's bad code on purpose. 32 00:01:26,370 --> 00:01:30,630 All right. So del my_list. 33 00:01:30,630 --> 00:01:32,940 Let's get rid of number three cuz true is there. 34 00:01:33,950 --> 00:01:38,930 And, let's get rid of number four cuz that's the abcdefg string. 35 00:01:40,010 --> 00:01:45,270 And let's get rid of six, which is the false. 36 00:01:45,270 --> 00:01:47,180 We don't want that false anymore. 37 00:01:47,180 --> 00:01:49,210 And then let's print out my list. 38 00:01:50,510 --> 00:01:52,682 I want to clean up this list, and then I want to print it out. 39 00:01:52,682 --> 00:01:53,591 Okay. 40 00:01:53,591 --> 00:02:00,631 Let's give this a try: python buggy. 41 00:02:00,631 --> 00:02:01,920 Oh, but what's this? 42 00:02:01,920 --> 00:02:03,390 I've got an index error. 43 00:02:03,390 --> 00:02:05,850 So I must've counted something wrong. 44 00:02:05,850 --> 00:02:08,650 So what I could do is I could come back up here and 45 00:02:08,650 --> 00:02:15,240 I could print my list and I could do this after every single step, 46 00:02:15,240 --> 00:02:19,650 and I could look at what's in there and I could go oh, that's where I messed up. 47 00:02:19,650 --> 00:02:21,530 I did something horrible there. 48 00:02:21,530 --> 00:02:26,680 But that's kind of sloppy, right? 49 00:02:26,680 --> 00:02:30,560 That kind of takes a long time, it's not efficient at all. 50 00:02:30,560 --> 00:02:35,355 So, instead, let's use pdb, let's use the Python debugger to see what's going on. 51 00:02:35,355 --> 00:02:36,382 Okay. 52 00:02:36,382 --> 00:02:38,830 So, the first thing we have to do is we have to import pdb. 53 00:02:40,020 --> 00:02:41,870 So, imports go at the top, right? 54 00:02:43,400 --> 00:02:45,200 So, there we go import pdb. 55 00:02:45,200 --> 00:02:51,170 And then, wherever in the code that I want to start my live, my interactive debugger. 56 00:02:51,170 --> 00:02:52,920 I'm gonna call, set trace. 57 00:02:52,920 --> 00:02:56,130 So this is like setting a break-point, in like a JavaScript debugger. 58 00:02:57,490 --> 00:03:00,190 I wanna jump in and start tracing the file from right here. 59 00:03:00,190 --> 00:03:02,410 So I'm gonna do that right before I start deleting things. 60 00:03:02,410 --> 00:03:05,690 I'm gonna call pdb.set_trace(). 61 00:03:07,220 --> 00:03:07,720 Okay. 62 00:03:09,190 --> 00:03:11,050 Everything's set up, yeah? 63 00:03:11,050 --> 00:03:12,370 It's all set up. 64 00:03:12,370 --> 00:03:14,470 It's all good. 65 00:03:14,470 --> 00:03:16,932 So, let's try running the script again. 66 00:03:16,932 --> 00:03:23,120 Okay so, python buggy.py, 67 00:03:23,120 --> 00:03:28,120 I didn't get an index there, but, I did get this weird new prompt, 68 00:03:28,120 --> 00:03:34,970 I've got this arrow pointing to del my list 3, then I've got this pdb prompt. 69 00:03:34,970 --> 00:03:36,930 So, lets deconstruct this a little bit. 70 00:03:36,930 --> 00:03:39,520 If we look here, there's this six. 71 00:03:40,970 --> 00:03:46,450 And if we look at our script right there and we have this 72 00:03:46,450 --> 00:03:50,640 arrow pointing to del my_list[3] which just happens to be on line six. 73 00:03:50,640 --> 00:03:55,040 so this is telling us that this is the next line we are going to run and 74 00:03:55,040 --> 00:03:55,630 it is line six. 75 00:03:57,290 --> 00:04:01,080 Okay, so, my list, del my_list[3] is line 6. 76 00:04:01,080 --> 00:04:03,050 That's the next line that's going to run. 77 00:04:03,050 --> 00:04:06,500 So what we're, what it's doing is it's letting us know what's coming next, right? 78 00:04:07,980 --> 00:04:14,060 So, the neat thing about pdb here is that first of all, pdb has a lot of options. 79 00:04:14,060 --> 00:04:15,910 We're not going to talk about all the options. 80 00:04:15,910 --> 00:04:17,370 There's a pep you can go read. 81 00:04:17,370 --> 00:04:20,270 There's documentation, tons of stuff. 82 00:04:20,270 --> 00:04:21,500 But while we're in here, 83 00:04:21,500 --> 00:04:24,530 it's almost exactly like being in our standard python shell. 84 00:04:24,530 --> 00:04:31,070 You know, we go in and we can do like, you know, help str, and we get stuff, right? 85 00:04:31,070 --> 00:04:35,230 So, we can do all sorts of things here, 86 00:04:35,230 --> 00:04:38,090 what's really cool is we can work with all of our variables. 87 00:04:38,090 --> 00:04:41,960 So let's see what my list looks like right now. 88 00:04:41,960 --> 00:04:44,940 And it looks just like the regular list. 89 00:04:44,940 --> 00:04:45,620 Right? 90 00:04:45,620 --> 00:04:47,020 Nothing has happened yet. 91 00:04:47,020 --> 00:04:51,380 We wanna let it run the next line, so we can type out next or 92 00:04:51,380 --> 00:04:54,890 we can just type n for next and press Enter. 93 00:04:54,890 --> 00:04:58,200 And there is why you don't wanna use single-letter variable names. 94 00:04:58,200 --> 00:04:59,730 What if you set a variable called n? 95 00:05:01,060 --> 00:05:02,300 And you're trying to check it out. 96 00:05:02,300 --> 00:05:06,060 There's ways to do it, I'll let you find that in the doc, but come on, 97 00:05:06,060 --> 00:05:06,800 that's annoying. 98 00:05:06,800 --> 00:05:11,330 Okay, so now it tells us that we are on line seven, and 99 00:05:11,330 --> 00:05:13,230 that this del my_list[4] is gonna be the next one. 100 00:05:13,230 --> 00:05:14,170 But before we do that, 101 00:05:14,170 --> 00:05:18,440 because we've got these index errors, we wanna see what's in my_list. 102 00:05:18,440 --> 00:05:19,370 So we look at my list. 103 00:05:19,370 --> 00:05:22,380 All right, great, the True is gone which we wanted to do. 104 00:05:25,090 --> 00:05:29,730 So we don't want to delete. 105 00:05:29,730 --> 00:05:31,650 Like this is going to delete number four, right? 106 00:05:31,650 --> 00:05:35,250 So zero, one, two, three, four. 107 00:05:35,250 --> 00:05:36,480 That's the three. 108 00:05:36,480 --> 00:05:37,670 We don't wanna delete the three. 109 00:05:37,670 --> 00:05:40,500 We want to delete the string. 110 00:05:40,500 --> 00:05:43,130 So, let's go and change our script. 111 00:05:45,490 --> 00:05:52,240 And notice that our pdb did not change. 112 00:05:52,240 --> 00:05:55,070 Our pb didn't restart or anything like that. 113 00:05:55,070 --> 00:05:56,040 It's just gonna keep running. 114 00:05:57,220 --> 00:05:59,710 So, okay, I wanna, I wanna fix this. 115 00:05:59,710 --> 00:06:02,940 So let's go in here and let's actually add in. 116 00:06:04,240 --> 00:06:08,350 Well, first of all here in pdb, let's type q to quit. 117 00:06:08,350 --> 00:06:09,420 So that gets us out of pdb. 118 00:06:09,420 --> 00:06:12,200 So let's go back up here and 119 00:06:12,200 --> 00:06:18,400 let's add in some comments so that we can keep track of what my list is. 120 00:06:18,400 --> 00:06:19,190 So I'm gonna copy it. 121 00:06:19,190 --> 00:06:22,000 And I'm gonna come down here. 122 00:06:22,000 --> 00:06:22,720 Alright. 123 00:06:22,720 --> 00:06:25,845 So at this point, we deleted true. 124 00:06:25,845 --> 00:06:26,960 'Kay? 125 00:06:26,960 --> 00:06:32,470 So down here we're gonna do this three again, which we 126 00:06:32,470 --> 00:06:34,780 deleted that one before and its gonna delete this one now. 127 00:06:36,360 --> 00:06:41,860 And then, let's go ahead and copy this down here, 128 00:06:41,860 --> 00:06:44,550 we wanna get rid of this False. 129 00:06:44,550 --> 00:06:51,530 So we've got zero, one, two, three, four. 130 00:06:51,530 --> 00:06:53,250 Alright. So, we want to del my_list[4]. 131 00:06:53,250 --> 00:06:55,940 And that's gonna get rid of the False. 132 00:06:58,120 --> 00:07:04,180 Now ideally, if I was actually writing this, 133 00:07:04,180 --> 00:07:06,610 I'd go and look for remove or something like that. 134 00:07:06,610 --> 00:07:09,000 Some other solution, right? 135 00:07:09,000 --> 00:07:11,000 You know, maybe I don't think of that. 136 00:07:11,000 --> 00:07:11,680 Right. 137 00:07:11,680 --> 00:07:14,050 So let's run our script again. 138 00:07:14,050 --> 00:07:14,550 So. 139 00:07:15,890 --> 00:07:16,990 Down here. 140 00:07:16,990 --> 00:07:19,590 And we're gonna run buggy again. 141 00:07:20,640 --> 00:07:22,270 'Kay so del my_list? 142 00:07:22,270 --> 00:07:23,560 Yep, that's good, let's do n. 143 00:07:23,560 --> 00:07:25,180 del my_list? 144 00:07:25,180 --> 00:07:25,970 Yep, that should be good. 145 00:07:25,970 --> 00:07:27,540 Let's do n. 146 00:07:27,540 --> 00:07:28,650 Yep, that should be good. 147 00:07:28,650 --> 00:07:31,920 Let's do n. So the next thing that's gonna run is 148 00:07:31,920 --> 00:07:32,800 this print(my_list). 149 00:07:32,800 --> 00:07:35,310 Well let's look at my_list before we print it. 150 00:07:35,310 --> 00:07:38,750 And look at that it's just numbers. 151 00:07:38,750 --> 00:07:40,028 It's exactly what I want. 152 00:07:40,028 --> 00:07:41,335 'Kay. 153 00:07:41,335 --> 00:07:45,540 So I wanna let the script just keep running like it normally would. 154 00:07:45,540 --> 00:07:47,890 We'll pretend the pdb wasn't even there. 155 00:07:47,890 --> 00:07:51,190 The way we do that is with using c for continue. 156 00:07:51,190 --> 00:07:53,250 Again, you can type out continue. 157 00:07:53,250 --> 00:07:54,100 We press Enter. 158 00:07:54,100 --> 00:07:57,738 It prints like it's supposed to and everything's good. 159 00:07:57,738 --> 00:07:58,238 'Kay. 160 00:07:59,290 --> 00:08:01,840 I have one very important thing I have to remember to do. 161 00:08:01,840 --> 00:08:06,800 probably the most important thing about using pdb, and that's I need to get rid of 162 00:08:06,800 --> 00:08:10,610 it, cause if I leave this in here and I give the script to somebody else, 163 00:08:10,610 --> 00:08:12,670 it's gonna hang on them and they're not gonna know why. 164 00:08:13,930 --> 00:08:15,119 So let's take that out. 165 00:08:15,119 --> 00:08:17,824 [BLANK_AUDIO] 166 00:08:17,824 --> 00:08:18,590 All right. 167 00:08:18,590 --> 00:08:20,780 So, now, my script is nice and clean. 168 00:08:20,780 --> 00:08:22,750 I do want to show one more thing, 169 00:08:22,750 --> 00:08:26,690 though, which is how you will commonly see pdb used. 170 00:08:26,690 --> 00:08:29,080 You can do the way that we did it, where we had the import at the top. 171 00:08:30,120 --> 00:08:30,740 We bring that back. 172 00:08:30,740 --> 00:08:33,860 Where you have the import at the top, and then the set_trace lower down. 173 00:08:33,860 --> 00:08:38,390 But what you will see most of the time is this. 174 00:08:41,700 --> 00:08:47,090 You will see import pdb; pdb.set_trace(), all in one line. 175 00:08:47,090 --> 00:08:48,350 Now this is valid. 176 00:08:48,350 --> 00:08:52,390 It's a little iffy as far as style goes, but it's perfectly valid. 177 00:08:52,390 --> 00:08:55,130 This is the only time. 178 00:08:55,130 --> 00:09:01,390 The only use I will ever tell you where its okay to use a semicolon in Python. 179 00:09:01,390 --> 00:09:05,770 Just wanted you to see this and not be surprised when you see this, otherwise. 180 00:09:05,770 --> 00:09:07,060 But our script is good, 181 00:09:07,060 --> 00:09:11,820 we don't need pdb anymore, we wanna get rid of it and save our file. 182 00:09:11,820 --> 00:09:15,960 Using the python debugger makes it a lot simpler to find mistakes in your code, and 183 00:09:15,960 --> 00:09:19,560 work through exactly what's going on at any given time. 184 00:09:19,560 --> 00:09:24,210 Also I'm sure you saw another reason not to use single letter variable names. 185 00:09:24,210 --> 00:09:27,210 If you use pdb, your variable names might be pdb commands.