Exercising Your New Knowledge
00:00 As with most things, but especially with classes, you need to exercise your new knowledge to make it stick and to include it—to be able to include it—as part of your tool kit when you go forward.
00:12 Do exercises, write small scripts that make use of classes, and notice whenever you’re using classes as well. The Python standard library relies heavily on object-oriented programming.
So here are some exercises for you to try. Modify the
Doggo class to include another attribute—
.coat_color, for example—and add that to the
Maybe also add that to the
Create a new class, a
Car class, with a few instances that would be appropriate for cars. Add a
.mileage attribute to the
Car class, and then add a
.drive() instance method to the class, which will add a number of miles to the mileage. So if you instantiate a car—I don’t know, a Ford—and then you say
ford.drive(), and you pass in, say,
10 to that method, if you look at the
ford.mileage, you’ll see that
10 has been added to the mileage, which might have started out at
0, or maybe the starting mileage is something that you can pass in to the constructor.
01:17 Already here you can see that you can really get creative with classes, and that’s the best thing to do to really get a grip on it. So good luck, and definitely do these exercises.
01:28 It’s one thing just to look through and say, yep, I understand that. Yep, I understand that. Yep, I understand that. But when you actually come to a blank script and have to actually write this stuff and get it working, that’s when you realize, oh, I didn’t understand that part. So definitely give that a shot.
Become a Member to join the conversation.