 Hey everybody, this is Brian. Welcome to the 8th Python tutorial. If you're new to programming, this is going to blow your mind. If you are an old programmer like me, this is probably the tutorial you've really been waiting for. Classes and objects. When we say a programming language like Python is object oriented, what does that really mean? What is an object? An object is anything. We've worked with objects with strings, integers. You could argue that an integer isn't actually an object in Python, but Python actually wraps it with an integer object similar to what Java does. So we work with objects all the time. Wouldn't it be great if we could create our own objects? Well, we can. We can create a class. What is a class? It's a blueprint. So we're just going to say class animal, and it's going to inherit, which we'll discuss here in just a second, the object. Notice how I said this is an object because everything is an object. You're an object. Your cat's an object. I'm an object. Don't call your girlfriend or wife an object. That's a conversation that won't end well. So we're going to say def eat. And I'll explain what self means in just a second. I just want to flesh this out a little bit so we can continue the conversation. And through the magic of copy and paste, we're going to make another function here. All right, so we have our animal class. We have a variable, and we have some definitions or functions as they're called inside of there. We covered that in the last tutorial. What is inheritance? Everything has to inherit from a base object. That's how everything is an object. So what exactly is inheritance? Well, we're going to make another class to illustrate that. If I could spell a mammal is an animal. For example, you're a human, which means you inherit from mammals, which inherit from animals. So you can have multiple inheritance in here. Not so much true multiple inheritance in the sense that some languages have. But you can actually, you know, get pretty complex here. Has backbone equal true. Has hair. You know, just some definitions of what really makes a mammal a mammal. Def grow hair. Notice how it says self. Self is a reference to the current object. And that's going to become pretty apparent here in just a second here. And if you're bald like me, you wish you could grow hair. All right, so now we've got two classes. The mammal class inherits the animal class, meaning it has all the properties of an animal. For example, I'm going to say cat. Yeah, you knew that was coming. Equals new. I shouldn't say that. I shouldn't say new. Sorry. I was thinking for my C++ days. I've had a really long day. And we're going to say dog equal mammal. Notice how they're both mammals. We're going to say cat.name equal shakes. My cat's name is Shakespeare. I call him shakes for short. Dog.name equal, and I used to have a dog named Molly. So these are two totally separate objects. They are instances of the mammal class. That's why I say class is a blueprint. This is not written in stone, meaning you can change it. You also notice how even though it's a mammal, we're accessing the name variable from the animal object. That's what inheritance is. Animal implicitly includes everything in the animal class because we're inheriting from it. That's how we can access name. And we can say cat.eat. Dog.sleep. And let's just run this. Why not? So you can see the eating, sleeping. It would probably be better if I put the name. Okay, let's actually do that just for illustrative purposes. I know you guys are smart enough to know what's going on here, but whoops. I don't know why I keep doing that. Oops, there we go. Hey, let's just... because I'm a huge fan of copy and paste. We said we were going to discuss the self variable, and this is where it really comes into play. You can see how shakes is eating and Molly is sleeping. We said cat.eat.dog.sleep. Self is a reference to the current object that we're accessing memory. Remember, this is a blueprint. So there's two animals in memory, technically mammals. Each one of these has a different name. One is shakes, one is Molly. So we have to access the self. Notice how we can't just say name. That generates an error, unresolved reference name. If we try to run it, you know, boom, the whole thing explodes. So you have to access the current object. And there are other ways around that, but for sake of simplicity, this is what we're really going to discuss. So how do you like that? Does that blow your mind or what? It is just dead simple in Python to make classes and objects, and to inherit. So you can see how a class has its own scope, similar to a definition. And the definition is inside the class, so it's part of the class scope. Now, you may be asking the question, well, if it's in the same scope, like here, why do you have to use self.name? Python does that to protect itself. When you have a lot of objects floating in memory, you need to know where they go and where they belong. So that's something you just need to bear in mind. And to kind of solidify this, let's actually make it a snake. You can probably hear my cat in the background. And we'll say, through the magic of copy and paste here, darn mouse, I need to buy a new mouse. I really need to buy a new mouse. Say it with me, ladies and gentlemen, buy a new mouse. Kitty, go away. Last time she was in here whining, we actually had an earthquake. Earthquakes are very rare in Michigan. I live in Michigan in the United States. And Kitty, go away. It was a 4.0 and this cat is deaf, so she was like scared. I feel really bad for her. All right, so we're going to now buy a cat.growhair dog.growhair snake. Notice there's no grow hair because snake is animal. And that is why you would use inheritance. You don't want a snake to grow hair because that would just be creepy. But a snake, you know, has eat, has sleep, has everything in the animal class, but has nothing in the mammal class. So now you understand how inheritance works. A mammal can inherit from an animal, but we did not inherit a mammal in the animal class. We inherited straight out of the object. Makes sense? I hope so because that's about the best description I can give you. That's all for this tutorial. I hope you found this educational and entertaining. There is more to come and there's a lot more to objects and classes than what meets the eye. I encourage you to do some research on your own. Some of the things we're going to discuss in future tutorials, and this is really just to kind of wet your appetite so you can get used to working with classes and objects because that's really the power of an object-oriented language. Be sure to visit my website for the source code for this and other tutorials under Python and join the Facebook Void Realms group. I know I've been beating that, you know, just senseless saying join the group, join the group, there's 200 of us in there. A lot of times it's really difficult to get a hold of me and then once you get a hold of me, my attention spans like a goldfish because I've got so many other messages coming in. Whereas you have 200 other programmers, some of them are more experienced than me that are willing to help.