1
00:00:00,420 --> 00:00:02,810
Let's talk about a very important topic.

2
00:00:02,820 --> 00:00:11,160
If you want to be a great programmer and that is called debugging you see nobody writes perfect programs.

3
00:00:11,160 --> 00:00:13,560
It's honestly impossible.

4
00:00:13,560 --> 00:00:17,820
We're always going to make mistakes and programs are constantly being improved.

5
00:00:17,880 --> 00:00:25,340
We constantly improve our code as we find more and more mistakes because programs are full of errors

6
00:00:25,410 --> 00:00:30,430
or what we call bugs or exceptions at runtime when the code is running.

7
00:00:30,430 --> 00:00:39,480
You see the act of finding and removing these bugs or errors from our code is called debugging and a

8
00:00:39,480 --> 00:00:46,770
big chunk of our time as programmers is debugging code whether it's our own code or other people's code

9
00:00:47,070 --> 00:00:50,000
and figuring out Hey how come this doesn't work.

10
00:00:50,190 --> 00:00:57,990
And the senior developers are really really good at debugging so let's talk about in this video about

11
00:00:57,990 --> 00:01:02,940
some of my recommendations for debugging and what some good practices are.

12
00:01:03,030 --> 00:01:08,220
Now keep in mind that you get better at debugging code as you code more and more so the best thing you

13
00:01:08,220 --> 00:01:13,310
can do is to just keep practicing keep writing Python code.

14
00:01:13,430 --> 00:01:15,110
So what are some of my tips.

15
00:01:15,110 --> 00:01:19,290
Well first you definitely want to use some lending.

16
00:01:19,640 --> 00:01:25,880
And what is limiting what we've talked about is right when we set up our developer environment limiting

17
00:01:26,390 --> 00:01:31,410
allows us to detect as we code some issues with our code.

18
00:01:31,490 --> 00:01:40,850
For example if I do num plus four here and I get a read underline I see here that the PI flakes Lintner

19
00:01:41,270 --> 00:01:44,210
notices before I even run my code that hate.

20
00:01:44,250 --> 00:01:46,120
There's an undefined named num.

21
00:01:46,700 --> 00:01:54,140
So linking allows us to find these errors before we even run our code and we saw that we can use I.D.

22
00:01:54,140 --> 00:02:01,520
like Pi charm that has linking already built in or use a sublime text or visual studio code and install

23
00:02:01,520 --> 00:02:07,140
packages and extensions like Pi lent to actually have lending in our code.

24
00:02:07,220 --> 00:02:09,720
That is a really really important tool.

25
00:02:09,800 --> 00:02:17,230
Next I recommend always using some sort of an idea or editor.

26
00:02:17,740 --> 00:02:23,470
Because these editors and ideas especially the ones that are specific for python like pie chart have

27
00:02:23,530 --> 00:02:29,430
all these built in tools for us to use such as auto formatting based on pep 8.

28
00:02:29,440 --> 00:02:34,750
Remember these style official style guide that the Python community has.

29
00:02:34,840 --> 00:02:43,790
It allows us to detect errors in our code with let's say highlighting and formatting of our code so

30
00:02:43,790 --> 00:02:48,410
it becomes easy for us to detect any errors before we even run the code.

31
00:02:48,410 --> 00:02:55,280
Finally you want to be able to learn to read errors and this is something that I tried to practice with

32
00:02:55,280 --> 00:02:55,520
you.

33
00:02:55,520 --> 00:03:06,670
As we've learned Python for example if I do four plus let's say gibberish here and I click Run right

34
00:03:06,670 --> 00:03:16,360
away I see Hey on line six there's an error in my main dot pi file there's a type error and I see here

35
00:03:16,360 --> 00:03:25,990
that I'm trying to add int and a string so we're able to read errors and understand what they mean maybe

36
00:03:25,990 --> 00:03:36,740
I do something like this if I click Run I get a syntax error and a syntax error I know means that hate

37
00:03:37,070 --> 00:03:42,530
Python interpreter is trying to read my code but what you just wrote is not python.

38
00:03:42,530 --> 00:03:45,940
I have no idea what you're doing here.

39
00:03:45,940 --> 00:03:49,240
I see that it's end of line while scanning string literal.

40
00:03:49,300 --> 00:03:54,580
So I'm trying to read this string and all of a sudden the program ends like you're supposed to end the

41
00:03:54,580 --> 00:03:54,980
string.

42
00:03:55,000 --> 00:04:01,420
Remember so being able to read these errors is really really important.

43
00:04:01,680 --> 00:04:04,130
And we've discussed some of them in this course.

44
00:04:04,200 --> 00:04:09,480
Some of the most common ones like syntax error where you're just writing something that Python interpreter

45
00:04:09,480 --> 00:04:10,340
doesn't understand.

46
00:04:10,980 --> 00:04:18,750
We have something like name error right where we use a variable that doesn't exist so Python doesn't

47
00:04:18,750 --> 00:04:19,190
understand.

48
00:04:19,200 --> 00:04:24,190
Hey I have no idea what name is what's what's the value of name.

49
00:04:24,220 --> 00:04:25,710
We have things like type error.

50
00:04:25,720 --> 00:04:34,550
That is when we mismatch data type and do let's say a string plus for we have things like value air

51
00:04:35,620 --> 00:04:42,160
we have things like here where we're trying to access a key that doesn't exist or attribute error where

52
00:04:42,160 --> 00:04:47,770
we're trying to access the method of a object and that method doesn't exist.

53
00:04:47,880 --> 00:04:53,520
Now these errors are something that we've been seeing throughout the course and we're going to see a

54
00:04:53,520 --> 00:04:54,570
few more.

55
00:04:54,670 --> 00:05:01,740
I would say that there's about 15 to 20 errors that are really really common that show up 90 percent

56
00:05:01,740 --> 00:05:03,270
of the time.

57
00:05:03,450 --> 00:05:07,360
And if you remember you can always go to the Python documentation to learn about them.

58
00:05:07,500 --> 00:05:09,740
But this just comes with practice.

59
00:05:09,900 --> 00:05:14,430
If you see an error that you've never seen before I recommend going to the documentation and reading

60
00:05:14,430 --> 00:05:15,230
up on it.

61
00:05:15,420 --> 00:05:22,800
Once you see it usually tend to remember when it comes up next but let me show you my favorite way to

62
00:05:22,800 --> 00:05:34,880
debug code and it's called P D B or Python debugger and PDB is a built in module in Python right here

63
00:05:36,320 --> 00:05:36,740
again.

64
00:05:36,800 --> 00:05:43,460
Remember our built in module are part of the standard library that Python comes with and PDB is the

65
00:05:43,460 --> 00:05:49,440
python debugger for interactive interpreters and you can read about it here.

66
00:05:49,520 --> 00:05:57,570
But let me show you what it does see PDB is extremely useful because it allows us to interact with the

67
00:05:57,570 --> 00:06:08,710
code so previously when we had a function let's say we have function ad and we have num one num two.

68
00:06:08,850 --> 00:06:18,320
And then this function simply returns num one plus num to now before if we wanted to let's say debug

69
00:06:18,320 --> 00:06:27,350
this code and let's say we're adding four plus some gibberish and it's giving us an error the first

70
00:06:27,350 --> 00:06:34,820
step that I would take is most likely adding a print in here so I could say print Hey what's what's

71
00:06:34,820 --> 00:06:40,890
going on let me see number one and number two and what it's giving and print is a really really quick

72
00:06:40,890 --> 00:06:43,530
way an easy way to debug your code.

73
00:06:43,710 --> 00:06:51,930
I use that all the time but PDB gives us a little extra boost so we can use it like this.

74
00:06:51,930 --> 00:07:00,810
We simply import PDB and instead of using something like a print function we can simply say let me add

75
00:07:00,810 --> 00:07:01,650
a space in here.

76
00:07:01,710 --> 00:07:11,180
We can simply say PDB for Python debugger and then say set trace and set trace I would say is the most

77
00:07:11,270 --> 00:07:16,480
useful method that we have on this PDB object that we import it.

78
00:07:16,580 --> 00:07:18,650
You see when I run set method

79
00:07:22,360 --> 00:07:28,930
it now gives me an interactive Python debugger that I can now type commands in here and actually test

80
00:07:28,930 --> 00:07:30,290
on my code.

81
00:07:30,460 --> 00:07:36,510
And here's the interesting part right now in this PDB I'm right inside of line eight.

82
00:07:36,560 --> 00:07:39,440
So my code tried to run but then it paused.

83
00:07:39,440 --> 00:07:45,620
As soon as it said PDB said trace and now I'm in this world and I can test out what's going on so I

84
00:07:45,620 --> 00:07:48,100
can actually say hey number one what is it.

85
00:07:48,350 --> 00:07:49,460
Well number one is four.

86
00:07:49,460 --> 00:07:50,910
What about num 2.

87
00:07:50,930 --> 00:07:57,050
Well num 2 is a string and I can be like oh I'm trying to add a string and an end to this this doesn't

88
00:07:57,050 --> 00:07:57,640
work.

89
00:07:57,740 --> 00:08:00,830
So I can detect errors this way as well.

90
00:08:00,830 --> 00:08:06,290
Now this PDB interactive actually has a ton of useful things.

91
00:08:06,290 --> 00:08:14,480
So if I type and help here you see that I have commands that I can use in the PDB.

92
00:08:14,480 --> 00:08:16,540
So I have a ton here.

93
00:08:16,610 --> 00:08:22,430
I have access to which I'm assuming is going to excite me out of this PDB Python debugger but I also

94
00:08:22,430 --> 00:08:26,590
have things like let's say list.

95
00:08:26,630 --> 00:08:27,880
Let's see what list does.

96
00:08:27,890 --> 00:08:34,480
So when I type in list here and right now the Web site is really not showing me too much but hopefully

97
00:08:34,490 --> 00:08:38,980
can still see list and I get some sort of an output.

98
00:08:38,990 --> 00:08:42,240
I see that I get a list of my code and.

99
00:08:42,430 --> 00:08:45,820
OK but let's say I don't know exactly what list does.

100
00:08:45,860 --> 00:08:55,820
Well PDB allows us to type help and then list and when we do that it actually gives us the documentation

101
00:08:55,910 --> 00:09:00,620
just like we have here if we go to a let's say list

102
00:09:03,400 --> 00:09:06,880
let's see list list list list right here.

103
00:09:06,880 --> 00:09:09,630
It gives me the documentation right in here.

104
00:09:09,670 --> 00:09:12,630
So it says list source for the current file.

105
00:09:12,680 --> 00:09:12,910
All right.

106
00:09:12,910 --> 00:09:15,730
So it just lists the source code that we wrote.

107
00:09:16,150 --> 00:09:17,160
OK let's go.

108
00:09:17,200 --> 00:09:19,240
What else is there.

109
00:09:19,240 --> 00:09:27,980
Well we also have something like step or S for short and step is really really cool let's actually use

110
00:09:27,980 --> 00:09:37,470
clear to bring this up to the top so I'm going to say clear so that it clears all breaks let's do let's

111
00:09:37,550 --> 00:09:39,470
make this a little bit bigger so you can see.

112
00:09:39,500 --> 00:09:39,920
There you go.

113
00:09:40,100 --> 00:09:40,690
That's better.

114
00:09:40,730 --> 00:09:47,170
Right so now I can actually do something like step and step is going to allow us to go to the next line.

115
00:09:47,180 --> 00:09:53,430
So again if we make this a little bit bigger step is going to say hey go to line nine.

116
00:09:53,550 --> 00:09:54,420
So let's see what happens.

117
00:09:54,420 --> 00:10:02,920
I'm going to type in step and I got step twice so let's try step and look at that.

118
00:10:02,920 --> 00:10:09,640
I go to the next line in my code and it gets me a you get a typo error the error that we got.

119
00:10:09,700 --> 00:10:11,250
So let's add another line here.

120
00:10:11,260 --> 00:10:16,480
Let's say that we're trying to add four plus five here or four.

121
00:10:16,480 --> 00:10:30,100
Multiply by five and assigning assign it to a T variable if I stop this and run this again I'm now inside.

122
00:10:30,200 --> 00:10:39,930
Over here if I type in t I get a name the t is not defined because while I'm inside the code here on

123
00:10:39,930 --> 00:10:40,350
line eight.

124
00:10:40,350 --> 00:10:42,720
But we have a red line nine yet.

125
00:10:42,810 --> 00:10:44,900
So let's do step.

126
00:10:45,330 --> 00:10:53,240
And now I'm on the next side so if I do t now I get Hey T equals 20.

127
00:10:53,300 --> 00:10:53,640
Awesome.

128
00:10:53,960 --> 00:10:58,200
So step I can step through the code which is really really useful.

129
00:10:58,220 --> 00:11:03,620
I also have things like continue which lets me continue through the code until I return something.

130
00:11:03,620 --> 00:11:06,800
And I continue and exit out of PDP and look at that.

131
00:11:06,830 --> 00:11:16,620
I get the air let's run this again another useful command is a and a actually gives me all the arguments

132
00:11:16,950 --> 00:11:18,720
of the current function that we're in.

133
00:11:18,720 --> 00:11:19,730
So look at that.

134
00:11:19,770 --> 00:11:21,150
I get all the arguments.

135
00:11:21,150 --> 00:11:23,420
Which again is really useful.

136
00:11:23,430 --> 00:11:29,860
There's also the command w which shows me the context of the current line.

137
00:11:29,870 --> 00:11:40,700
It is executing so you can see here where we are we're in the add function we called it with these arguments

138
00:11:41,600 --> 00:11:45,910
and we're now about to run this line.

139
00:11:46,130 --> 00:11:51,770
And then finally as we're debugging this all right I think I finally see that we have an error we should

140
00:11:51,770 --> 00:11:59,830
have given it a number so I can now just exit out of here and simply fix this to five.

141
00:11:59,870 --> 00:12:07,760
So now I can just remove my PDB and everything should be working fine with my code awesome.

142
00:12:07,990 --> 00:12:12,790
So PDB is awesome to step through your code and debug and try different things.

143
00:12:12,790 --> 00:12:19,900
It does require a bit of practice but after a day of just playing around with PDB you will become really

144
00:12:19,900 --> 00:12:21,130
really good at debugging.

145
00:12:21,160 --> 00:12:28,750
So after this video I recommend that you write PDB and hopefully you have a large enough python file

146
00:12:28,750 --> 00:12:35,710
they can just step over and experiment with different commands using help using list to see what you

147
00:12:35,710 --> 00:12:40,450
have available to you actually want to show you one last thing.

148
00:12:40,480 --> 00:12:49,960
So let's say we go back to our code our PDB said trace I click Run could I do something like this where

149
00:12:50,110 --> 00:12:53,460
I noticed that I Taipei and I have.

150
00:12:53,500 --> 00:13:02,260
All right my code is working but could I change numb to two now equal some gibberish.

151
00:13:02,390 --> 00:13:08,620
Well let's see if I do next now let's do next to run this final line.

152
00:13:10,260 --> 00:13:11,180
I get an error.

153
00:13:11,460 --> 00:13:19,770
So I'm also able to change variables in here and write Python code to perhaps test out my functions.

154
00:13:19,800 --> 00:13:26,740
This is now working I can test out hey what happens if we change this variable to another value again.

155
00:13:26,840 --> 00:13:27,890
Very very useful.
