Loading video player…

Understanding What's a Mixin

00:00 To understand what’s a mixin class, start by looking at the class Person defined on the screen. To create an instance of the class Person, you need to specify a name and an age, and then you save both in their respective attributes.

00:14 On top of that, the class Person defines a method .as_dict() that returns a dictionary that represents a serialization of an instance of the class Person.

00:25 To see this method in action, you could create, for example, an instance of Person with the name John and age 42, and if you call the method .as_dict(), you get a dictionary with two keys, name and age, and the respective values, John as a string and the integer 42.

00:44 Completely unrelated to the class Person is the class Car that you can now see on the screen. Instances of the class Car are created with a make and a model. And it just so happens that the class Car also defines a method called .as_dict() that returns a dictionary that represents a serialization of an instance of the class Car.

01:06 To see this in action, you can create an instance of the class Car with the make Toyota and the model Prius. You can call the method .as_dict() and then you get a dictionary with two keys, the keys make and model, and the corresponding values regarding the instance or this specific instance of the class Car.

01:24 Now, something you might have noticed across both the class Person and the class Car is that the method .as_dict() is structurally the same in both classes.

01:35 The implementation is not literally the same, the code is slightly different, but the structure of what the method is doing feels pretty much the same. So you might be wondering, isn’t there a way for me to reuse the same piece of code across the classes Person, Car, and any other classes you create in the future that might also want to be serialized as a dictionary?

01:59 And this is precisely the type of idea where mixin classes come into play, because the key idea for mixin classes is that they encapsulate reusable behavior that you might want to use across different unrelated classes.

02:15 So that’s what a mixin class does. You define a couple of methods, typically one, two, or a small number of methods that stand pretty much on their own, and then the fact that they’re defined on a class makes it easier to reuse across other unrelated classes through inheritance.

02:34 You’ll learn more about this, but mixin classes use inheritance not because you want to create a strict object-oriented programming hierarchy, but because inheritance is the mechanism that Python uses to share methods from superclasses to subclasses.

02:52 You’re going to leverage the mechanism of inheritance, but pay attention because inheriting the mixin class is not the goal, it’s just the means through which you get this reusable behavior.

03:05 But you’ll understand this better in the next lesson when you write your first mixin class.

Become a Member to join the conversation.