Effective PyCharm Transcripts
Chapter: Debugging Python applications
Lecture: Concept: Debugging

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We have a whole variety of ways of viewing variable state. When we stop in the debugger obviously down here in the bottom right,
0:08 we've got the watch list of variables. So we've got our creatures and if it's something interesting like a list we can expand
0:15 it out and see more details. We've got our command, we've got the active creature turns out to be a bat
0:20 of level three right now. So while this is fine, it doesn't really show you in context what's happening, doesn't show you here this value.
0:29 We accept it from. The user is an A right on the line where we did the input and so on. So up here we can see way more information.
0:37 We can see that we have this great text. This is not a comment, this is something that debugger puts here says here is
0:44 a hero And the text representation of that is a creature. Gandalf of level 75. And we saw that we can even highlight it and pull
0:52 it down basically the same interaction we have in the watch window Over here we have
0:57 the active creature, that's a bat of level three and the color is important.
1:01 Remember the hero is great because that's the value has always had since we've recently looked
1:07 at it, but this creature has turned orange and that means it's changed last time through the loop, it was something else.
1:14 Who knows what a toad or a dragon or whatever it is in our little story here, But now it's become a creature a bat of level three.
1:23 And so the fact that it changed, orange tells you not just what is its value, but that it has somehow been updated over here.
1:30 You can see there's this input that we got from the user, what did they type for this particular run of the game loop?
1:37 They typed A so they're going to be attacking, What's going to do it here? Oh, Gandalf of level 75 is going to be attacking a creature bat of level
1:44 three super cool. The way we can put all this together, seeing the values is great. But sometimes you might want to change the situation here
1:53 We've asked the user to input something. What do you want to do? Attack run away or look around. They said attack like oh, you know,
2:00 we really should have typed run away because that's the scenario we're trying to test.
2:05 Well let's just change it. There's all these situations and programs that are hard to come up with exactly the right situation.
2:14 So you can use the debugger to change it. So if we go down to the command and the variable window and choose set value
2:20 or even to the gray part that the debugger put in and pull it down and say set value, we can change it.
2:26 What if we put in [r] if we set this value and change into [r] what happens even though it says in the console up here. What do you want to do?
2:34 Attack or runaway or look around, we set attack, but then all of a sudden the wizard has become unsure of his power and flees Why?
2:41 Because we changed what happens. That's what happens if he were to run away.
2:45 So very cool that we're able to sort of carefully control the flow for different situations to build up exactly what we want to test,
2:55 It's pretty obvious you can watch variables, but you can also come over here and hit the plus and add a watch expression So I want to know any time.
3:03 Is there an active creature whose level is greater than 15? Yes or no. We can watch that expression and you can put all sorts of
3:11 interesting things like some database level call that says is the current user and admin yes
3:17 or no. Right, That's not necessarily a property of the the current user object. Although maybe if it's not though you can still check that here in this
3:26 evaluation watch area. So it's not just about watching variables,
3:30 even though quite literally the title of the Windows variables really it's variables plus expressions.
3:39 We saw the breakpoints are incredibly powerful here, we have two break points on our creature wizard game, we want to add a new one.
3:46 You click in the gutter, Just the right of where the line numbers are,
3:49 if you're showing them and that will create a default on off type of breakpoint is going to stop every time you hit it.
3:57 We saw that if you right click on it though, you can actually change this to be something conditional. So imagine in this scenario we described before,
4:05 I want to only hit the breakpoint if the user says R for run away. So the first bit of this test, if it's a else if it's it and or whatever,
4:15 I'm going to put a breakpoint here and set the condition to 'cmd==r' right. Just a standard Python conditional. If that ever evaluates to true,
4:24 we stop. So then we'll skip over all the other cases that are not running
4:28 away scenarios and not have to deal with jumping out of the debugger too much.
4:33 Remember this gives you auto complete and you can do pretty advanced Python right there.
4:38 If you expand it, you can do even more like check the evaluate and log
4:41 then print out some sort of computed message with details about what's happening every time the breakpoint hit. And if you uncheck suspend,
4:50 that will basically become a breakpoint. That just logs out messages. Remember once you've got a conditional breakpoint,
4:56 it has this little question mark by it. So normally it would just be a circle But now because that question mark,
5:03 that means it's conditional. Be careful if you click on it without right clicking, you're just going to disable it.
5:09 It will go away and well who knows what the condition was, but it's gone. So just be careful to not wipe these away unless you're sure
5:16 you're done with them. Finally, if we turn this into a breakpoint that only logs messages,
5:22 it turns yellow so you can see here we have suspend unchecked but evaluating log is turned on and we have this little F.
5:30 String. The command is whatever they've typed the creature is the name of the active
5:34 creature. And when we do this we get these little messages here like breakpoint reached The command is 'a' and the active creatures.
5:42 The Tiger command is a active creature is a dragon and so on. Remember in my example I put like some stars or something to make it stand out
5:50 because here without these boxes it's super hard to see what is the breakpoint and what is actually just program output around it.
5:58 So be sure to add a little bit something to make these stand out.

Talk Python's Mastodon Michael Kennedy's Mastodon