 Okay, good morning everybody, welcome to Intro to Class Programming in Python with Tokyo EdTech. That is me. Today's topics include classes, class methods, objects, instantiation, and inheritance. But first, a quick shout out to Kevin, first of his name, Lord of the 16-bit members, my first ever channel member. And a shout out to Paul, first of his name, Lord of the 8-bit members, my second ever channel member. Thank you so much guys. I really, truly appreciate it. Now classes are a topic of the day. Classes seem very mysterious to beginners, but they're actually fairly straightforward. So let's say, well first of all, a class defines an object. So let's say we have a person. So what are some things that we know about a person? So for example, a person has a name, a person has an age. So we'll stick with those two for now. So to define a class, we use the class keyword, and we give our class a name. Now note the name here, person, and it is capitalized. That is a Python convention. It'll work if it's not capitalized, but all the other Python programs will make fun of you. And when we create a class object, which I'll get to in a second, we need to instantiate that object. And so in this case, we're using a function called initialize, or init. Now be very, very careful here. This is two underscores, not one, two. That's two before and two after. Invariably I'm going to get somebody who's going to ask me why their code isn't working. It's because they have one underscore before and after. And in the parentheses, I need to put self. So I'm going to say self.name. For now, we're just going to leave it blank. And self.age equals zero, we're going to give it some default values. So we've said that a person, this is what a person has, they have a name and an age. So I'm going to go ahead and create some objects. So I'm going to say, Bob equals person. So I'm saying that Bob is a person. And he is what we say a member of the person class. So what happens when this statement is executed? So the computer says, okay, class person. Okay, I need to initialize this object, Bob. And Bob's name is blank and Bob's age is zero. So then what I can do is I'm going to say, Bob.name equals Bob. And Bob.age equals, let's say 40, I'm not sure how Bob is now. And then I'm going to make a second person, suit. And again, the computer program, Python says, okay, person, what's a person? Well, when we initialize a person, we say that we set their name to blank, we set their age to zero. So the reason self is used. So in this case, Bob is self. So Bob is the new object, Bob's name is blank, Bob's age is zero. Sue goes here, so self is Sue, Sue's name is blank, Sue's age is zero. And so I can change that here and sue.age equals, I'm going to make her 17. And then again, we'll go ahead and do Jason, Jason equals person. Jason.name equals, of course, Jason. And Jason.age equals 45. So just real quick, this Bob has nothing to do with this Bob, it's just a convenience for me as a human being. Bob's real name could be Robert, for example, but we'll change it back to Bob. And this could be, this could be x47, the computer doesn't know the computer doesn't care. Okay, it's just, it's just numbers to the computer. But I'm using Bob because that's what makes sense to me as a human being. So now what I can do now that I've instantiated my objects, I've given values to their names, I can now go ahead and print out that information. Now note, I'm using an F string here, this was added in Python, I think 3.7. So if you're using an earlier version, upgrade, okay, or update. So I want to say Bob.name is Bob.age, years old. So to access this information, I just use the object name, dot, the attributes. And if I run this, if I'll get no errors, you see here, Bob is 40 years old. Now I'm going to go ahead and copy that and go ahead and take care of that for Sue. And take care of that for Jason. Okay, so let's go ahead and run that one more time. And let's scroll it up. So you see Bob is 40 years old Sue is 17 years old and Jason is 45 years old. So this is the first, very first basic idea of objects in classes. Okay, so this is the class, which is person. And Bob, Sue, and Jason are objects. They have the same attributes as everything in the person class. Now what makes this nice is let's say I want to add location. I can say self.location equals that to each person. Or self.country, self.nationality. Now anything that I want to add to all of the class members, I just need to put in one place. I'm going to have to do that because we've got a little bit of time here. Let's say here, computer is acting a little iffy. I don't know if you guys saw that, but it is still running. Okay, that's a good sign. All right, so that was a little weird. Anyway, yeah, okay, now you can see how this would be kind of inefficient. So we have to declare, create the object, then add the information. But what we can do is we can pass the values here during the instantiation process. So I'm going to put here, I'm going to put name and age. And here name, oops, and here age. So don't get confused. This name and this name are the same. This age and this age are the same. But this name is a property of the class. Okay, so I'm just passing these values into the initialization method. So what I do now is I put bob and 40. Now I can delete this part. I'm going to put su 17. Now if I don't put those in, I'm going to get an error. So you have to make sure, just like with normal functions, you have to make sure that it matches. Now it's a little weird because you've got self here. So you're thinking got one, two, three, but you've only got one, two here. But remember, this self comes from here. So that's a little bit, that's a little bit, one of them, I think to me, one of the confusing things for beginners, and say Jason 45 and delete that code. Now what I'm going to do is I'm just going to test this and make sure I get the same result. Can I do so, so far so good. Now this is where it gets kind of interesting. So that's classes, objects, and instantiation. Now where the real power of object-oriented programming comes in is with subclasses or sometimes we have them called child classes and inheritance, which is a pretty advanced topic, but kind of simple in a way if you think about it. So let's say I want to make a new class called student. Now students kind of make sense. Students are also people. So it stands to reason that a student would have a name, a student would have an age. So what I can do is I can do the following. I can say person, and then I'm just going to go ahead and copy this. Now because I want to save some space, I want to use this, I want to inherit, well thought that second. So what I'm going to do is I'm going to inherit name and age from the person class. So what I have to do is also instantiate the parent. So this is where it kind of got a little weird. So what happens now? So I'm going to say sue is a student. So I'm going to instantiate sue. So this is going to be sue. This is going to be sue, this is going to be age 17. Then I need to initialize the parent class. So self, name, age. So now if I run this, it should be the exact same output. Okay, and it is. Now why I would do this, one of the reasons is let's say now I can say that self.grade equals, let's say we don't know grade this person said. Now I can add an attribute just for that class. So I can say print sue is in grade, now what would make sense is if I expanded this a little bit and I'll say sue is in 12th grade. So then in my init method here, I'm going to put grade and then down here I'll put grade. So you can see what I'm doing here. A student is a person. So that means a student has a name and an age, but a student only has a grade. So I'm saying students are in first grade, second grade, third grade, etc, etc, etc. And then that is how I create those objects. So you can see sue is 17 years old, sue is in grade 12. So rather than have to put name, age here, and then just have a separate set of code for that, I only need it in one place. So now, again, as I mentioned earlier, if I wanted to add something like location, as soon as I add it to person, the student class also gets access to that variable. So this concept where the child class or the subclass, how you want to call it, inherits, this is called inheritance, the attributes and methods, which we'll get to in a second of the parent class. And then what I'm going to do for, I'm going to make Jason, I'm going to make him a teacher. Again, a teacher is a person. I'm just going to go ahead and copy this. Because once you have it working once, copying stuff just makes it easier. So I'm going to make this into teacher. Teacher is a person. And I'm going to put for a teacher, I'm going to give them a department. Now note, I'm still initializing the parent class, and I need the name and the age. And I'm going to say self.department equals department. And so when I create my Jason object, Jason is an art teacher in the art department. And I'm going to go ahead and say printf, excuse me, Jason.name, teaches in the Jason.department. Let me go ahead and run that, locked up again. I think I'm going to have to close that. Oops. Hopefully it's still recording. Oh, the whole thing is locked up. Let's see what happens here. Okay, I got an internal error. I can't send it. Don't send. Okay. So we're back. Hopefully that's still recording. I don't even know this point. And let's go ahead and run it and see what happens. It says, in it takes three positional arguments, but four were given. So what did I do wrong here? Ah, I forgot to change this to teacher. Okay. Okay. So you can see now the output here. So now what I can do with this, well, I should just leave it like that for now. So the last thing we can do is we can create methods. So for example, so this is a method for the person class. And then if he says something, we're just going to say self dot, oops, don't forget the F, the F string, self dot name says, so then what I can do is I can say Bob dot say something and then I can do, separate that out so we can see a little better, sue dot say something and then Jason dot say something. So I'm going to run that and you can see now Bob says hi, sue says hi and Jason says hi. So all of the classes here, so the person class and the student class inherits this method from the person class. The teacher also inherits. So when the person says hi, when this person says something, they're just going to say hi. What I can do is I can actually override that. So I make a method with the same name and the same arguments here. And maybe, let's see, maybe what's something a student would say, says, is this on the test? And then a teacher say, teacher might say, yeah, I was going to put, did you bring your pencil? But I might, in this day and age would say, please mute your mic. Please mute your mic, something a teacher might say. So if I run that, you can see now Bob still says hi, sue says, is this on the test? And Jason says, please mute your mic based on which class they are in. So person, student or teacher. Last thing I want to show you before we finish this up is something I like to do to simplify some things. So what I can do is I can make a list of people and I'm going to put Bob, Sue and Jason. No, there are no quotation marks here because these are not strings. These are object names. So for person and people. Now this statement works for all of the objects. So I'm going to put print person is person years old. And then we got a new method. So if is instance, person and student, say print f, oops, print f quotes, person dot name is in grade, person dot grade, LF is instance. Person teacher, print f person dot name is in the person dot department. And then finally, all of them, we can do person dot say something. I'm just going to print a little extra line here after each person. So now if I get rid of all this stuff here, and I run it, I should get basically the same result. So for each of the objects, I'm printing this line because they all have that property. And then only if it's an instance of the student class, will we print the grade because only the students have a grade. Otherwise if it's an instance of the teacher class, we print the department the person is in. And then for all of the objects, we call the say something method. So that is classes in a nutshell. That's basically how classes are created. That's how they're instantiated. And that is how inheritance works. So this is a very, very powerful, very important programming method. So I hope this was helpful, and we'll at least get you started understanding the basic concepts of classes, methods and class inheritance. So thanks for watching. Please subscribe or even better become a channel member in the links below. Take care and keep on coding.