5.6 Program with classes - Video Tutorials & Practice Problems
Video duration:
10m
Play a video:
<v ->So let's stop coding for now</v> and think a little bit higher level. And I'm gonna show you how to program with classes. So this is something that's very common or that is required in Java programming. It's an object oriented language and in Python it's more optional. But you'll need to know it in case you're using a library that makes use of classes. So there's an old philosophical debate about what is a horse. Is the idea of a horse actually a horse or is it only the material example of a horse. Like an instance that you can touch and feel. And this brings us into objects in programming because both of those are concepts that we'll look at. So the first one is a class, this concept of class. And it would be like the idea of a horse. You can imagine it in your mind but it's not actually a physical, tangible thing. Each class can have a number of properties. So for example, a horse has certain properties like the breed and the category of horse. I don't know, like the type. Its age, its color, things like that. So these are attributes that the horse has. But then there are also methods. And so these are the things that the horse can do. So it can trot, canter, gallop, jump, eat, sleep and these would be like the string methods we looked at earlier. You can write your own methods on your own object class. And then there's the actual object or instance and so that's when you instantiate a class and then turn it into an actual thing that can do stuff. So in our word game that we created in lesson four we can program it as a class instead of as a bunch of functions and that class will have a few things that associate with it. So, setup, max wrong guesses, the word to guess, the letters that have been guessed, start game and the display word. So if it's all underscores or the letters are filled in. And so these are split up into methods and properties. So methods are what it does. So setup would be a method that you call to get it started and choose a random word and start game, and maybe display word. That could be either a property where you're getting what the word is to display or you can actually tell it to display it. And then properties are things that it has. So the word to guess would be a property, the max wrong guesses, the wrong guesses left, the guessed letters, things like that. So let's now look at the word guess. You can see here we have challenge 3e, word guess with classes. We can open that up. And I've written a bit of a lengthy thing here about an explanation for what's in this file. But we'll talk through it right now. So to define a class instead of saying def to define a function, we use the keyword class. And then we give it a name. And this name is normally title case. So the different words in it would start with a capital letter as opposed to snake case which is normally used for functions and variables. That would be all lower case with words separated by underscores. So class is normally title case and then parenthesis. And then this is saying that it is like an object. It's inheriting from the base object class. And then inside of this class, everything that's indented belongs to this class. And this is where you define your methods and your properties. So the setup method would refer to this init method. This is a little strange. It says underscore underscore init and then another two underscores. This is called a Dunder method and it has these underscores because you don't usually call it directly. It gets called in a different way and in this class init gets called by just instantiating word game with the class name and then some parenthesis. So when you do this it's actually gonna call this initializer function. And then we can set some properties on it. Self refers to the object or class that it's in. So it's like saying my, like if I'm the word game, my answer will be set to and then I have this function to pick a random word. And it's a function that opens a file and reads a word. And then also my num wrong guesses left is six. And then my guessed letters list is the empty list. So this is all run when you first create a new word game. So with this you can actually have multiple word games happening at a time. We've also got display words so this is a method that will return the display word. This is a play game method, yeah play game, start game, and it loops through and does all the stuff and it calls self get display word because now it's a method so this is the object and then dot and then you're calling the method. So now if a method wants to access a property of the object it can just say self. get the property, or call the method. And then one last thing that we have here is this property decorator. So this is a method, user did win. So if we got rid of this we would have to call it here, user did win and we would call it like a method. But if we have that property decorator this is actually a property and it's not callable. You just access it like it's any other property, like self.answer or self.guessed letters. So you can't do this for things that you pass values into, the method. If we had get display word and wanted to pass something in, like uppercase equals true and get display word was able to take in an uppercase property and use it, you wouldn't be able to turn this into a property. And then how you actually use a class, so here we are instantiating the class. So now game is an instance. It's like the actual, physical horse. And we can now tell it to do stuff. So we can say game.play game and we can get game.answer if we wanted to but that would be cheating. So if you ever see this kind of uppercase or title case word with some parenthesis in a library, you know that you're calling a function and that function is going to have methods and properties associated with it. So beyond just how to create and instantiate a class there's a few other concepts with object oriented programming. So one is this idea of abstractions and inheritance. So each of these trees could have a different class and then you instantiate them and one class will create a palm tree and one class will create a pine tree. But they are all trees and so you can abstract some of their common properties and methods out, like photosynthesize, into a parent class. Like a tree superclass that then these classes inherit from. So that's a common technique in object oriented programming to be aware about. Say we wanted to model these different displays on a class. They are a little bit different, they all have a height and a width property but those values would change for each different instance. So there are a lot of similarities but there are also some differences. So this also might be a time when you might want to use an abstract class, or a superclass or parent class and then inherit the child classes.