 Welcome back everyone. This is Brian and we're going to talk about multiple inheritance. We've covered classes. We've covered inheriting from another class. What if you have multiple classes you want to inherit from? That's exactly what we're going to do. Something which is don't even allow this because it gets so complex. Let's dive in and take a look. Let's go ahead and make a class here. So we're going to make a vehicle class. Let's say class vehicle. And if you remember from the last video, I definitely love to make sure that the instances of the variables are actually created so we don't get that nasty not defined error. I'm going to go ahead and say drive. Notice how we're not putting in a knit here. We simply don't need it. We're going to leave the default constructor. And I want to say self comma, and then speed. We're going to keep this just super ridiculously simple. I'm going to say drive. We're going to set the speed. We're going to just copy and paste this and I want another function called stop. We're going to set the speed to zero. We don't need that parameter there. Now let's go ahead and make another function called display. And this is where things are going to get a little bit interesting. Take special note of this function because we're going to do this function again in another class. I'm going to say print. And we're just going to simply say driving at whatever the speed is speed. Very simple little self contained class here. Let's go ahead and make another very, very simple class. I'm just going to make a freezer class. And this thing exists solely to well freeze food. There's a freezer. And let's go ahead and make a variable. Let's call it temp equals zero. So we're just going to set the temperature to zero by default here. Now I'm going to say def freeze. So we're going to be able to freeze some food at a specific temperature. I'm going to say self that temp equals whatever temperature we just handed it. And let's go ahead print out freezing. Just so we know that the freezer is actually going to shockingly freeze the food. Now I'm going to actually grab this right here from the vehicle class. Def display self. And we're going to modify this, but we're going to put it in our freezer class. You don't have to copy and paste it just as long as the name is the same. What we're going to do is intentionally create a naming collision between these two classes. So both the vehicle and the freezer have a display function. I must say freezing at self.temp because there is no speed in the freezer class. Very, very simple, very easy to understand. So what we can do is call these independently and call display and see exactly what's going on in the hood. Now let's see what happens if we use multiple inheritance and smash these two together. Okay, let's go ahead and combine these two together. We're going to make a freezer truck class and say class freezer truck. And we want a freezer and a vehicle. We're going to smash those into one giant class and initially I'm just going to say pass. So we're just declaring the class and it does nothing else. That's what past does in Python. And we're going to go ahead and create it. I'm going to say t equals the freezer truck. t.drive. And we want to go at 50 miles per hour. I'm here in the States. That's actually relatively fast. And then we're going to say t.freeze. I'm going to put this at negative 30. Because whatever we're carrying, we don't want it to, you know, thaw out on the way to the store. Then I'm just going to go ahead and print. And we are going to print out some dashes here just to kind of separate. Now we want to say t display and pop quiz. We have multiple displays. We have the freezer class display and the vehicle display. Which one of these is going to be displayed? So we're either going to see freezing at or driving at which one is going to happen here. Let's find out, save, run, freezing at negative 30. And we don't get the speed. We don't see that driving at. So now we've got multiple problems. We want to be able to see both of those. But what we've defined, whether we realize it or not, is a new concept called the method resolution order or the MRO. And you'll hear this time and time again, you'll see people in forums asking why you access or why this is happening. So the order is very simple. It's first come, first serve. So we defined freezer first. Therefore it's going to display the freezer. And if we just switch these around, we're still doing the same thing. We're still inheriting both of these. But watch what happens down here. Driving at 50 speed. So that is the method resolution order. It's first come, first serve. That is frustrating. So how do we get past this now? Let's say def display itself. And let's go ahead and let's print these out. Let's say print f. I want to show you a little trick here. I want to make sure these are actually subclasses. I want to say is a freezer. I want to say is subclass. I want freezer truck. So basically we're giving it the current class we're saying. So it's freezer truck, a freezer. And we're just going to say is a vehicle. And grab that. Good on here. Bang. Watch what happens when we call display now. Is a freezer true? Is a vehicle true? So now we are calling our own function and we can actually determine whether or not it's inheriting a specific class. The class you want to check is always first. The class you want to check against is second. So now let's go ahead and see how we would get around this. I'm going to say super. So we're going to call the parent. The problem is we have two parents now. So let's try calling them independently. Say freezer. I must sell display. And I'm going to take the same thing, but we're going to use the vehicle. So we're saying super, call the vehicle, super, call the freezer using the self object, the current object and call display directly and see, let's just see what happens here. Uh-oh. We had a bad time. So let's look at this in depth here. So on line 42, T display. So this guy down here and then it's jumping up to line 36, which is this guy right here. Super as super object has no attribute display. Now wait just a minute, we do. So if we look at freezer, it's right there. What is going on here? Well, again, method resolution order is popping in and saying, nope, can't do that. So just to prove it, let's comment up freezer as vehicles first. Let's run this again. Everything magically works. That is super frustrating. All right. So let's go ahead and just comment these out. This is not what I considered the correct way to do it, but I'm going to put some comments in here just in case somebody grabs the source code here. So what's the best way or the correct way of doing this? Well, we're going to call that class directly and there are other ways, probably even better ways, but this is what we're going to do with our limited knowledge set of Python at this moment. We're going to say freezer dot display. Remember what I said when we inherit an object, Python's creating it. So it knows that the freezer truck has a freezer in it. So we're going to call freezer display. We're going to call vehicle display. And let's watch this work. Now we can see we are freezing and we are driving. Oh my gosh, super confusing. So the major takeaways from this video is Python does allow a multiple inheritance, but it has some gotchas, mainly naming collisions. For example, we saw that the vehicle and the freezer have a display function. But once we inherit both of them, MRO steps in and says we're only going to use the first one. So if we want to use multiple, we have to independently call them. Super challenging. But once we wrap our head around it, it's also very elegant. I hope you enjoyed this video. You can find the source code out on github.com. If you need additional help, myself and thousands of other developers are hanging out in the Voidrealms Facebook group. This is a large group with lots of developers and we talk about everything technology related, not just the technology that you just watched. And if you want official training, I do develop courses out on udemy.com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there. You can drop me a note. I'm either working on it or I will actually develop it. I will put a link down below for all three of those. And as always, help me help you smash that like and subscribe button. The more popular these videos become, the more I'll create and publish out on YouTube. Thank you for watching.