Loading video player…

Writing a Mixin

00:00 The best way to understand what’s a mixin is to write a mixin. So go ahead and open the file as_dict_mixin.py that you downloaded from the reference materials. This file should contain the implementations of the classes Person and Car from the previous lesson, and what you’ll do now is take the methods .as_dict(), delete them from each class, so you delete Person.as_dict() and you delete Car.as_dict(), and you’re going to write a mixin class that implements a general method .as_dict() that can be reused for the classes Person, Car, and any other classes you create in the future. Go ahead and start by creating a class called AsDictMixin.

00:47 It’s a convention to use the suffix mixin in the class name to make it abundantly clear for everyone involved that the class AsDictMixin is supposed to be used as a mixin class.

00:58 Now mixin classes do not stand on their own, so typically your mixin class will not define the dunder method in it. It’s easier to define mixin classes that are stateless, that do not have any attributes of their own.

01:14 Instead, what you want to do is define the method .as_dict(), which takes no arguments, and is supposed to return a serialization of self as a dictionary.

01:25 Now how can you write a method .as_dict() that behaves like the methods Person.as_dict() and Car.as_dict() that you saw, without knowing anything about the instance on which this method is running?

01:39 Well, thankfully, Python has a built-in called vars(), V-A-R-S, that returns a dictionary with all of the attributes of the object you pass in along with its values. So a simple enough implementation would be to write return vars(self).

01:58 This isn’t necessarily the most robust implementation of a serialization method, but for the purposes of understanding what a mixin class is, this will do.

02:08 The method .as_dict() is defined in the class AsDictMixin. Now to make the classes Person and Car be able to use that method, you need to inherit from AsDictMixin.

02:22 So you’re going to make the class Person inherit from AsDictMixin, and you’re going to make the class Car inherit from AsDictMixin.

02:33 Strictly speaking, this means that both Person and Car are now subclasses of the same common class AsDictMixin. But remember that the main idea is not to create a rigid OOP hierarchy, it’s just to use the mechanism of inheritance to allow sharing behavior. So when you look at Person inheriting from AsDictMixin, you wouldn’t think about it as a Person is a AsDictMixin.

03:02 No, you just think that the class Person reuses the behavior from AsDictMixin. And now to see this in action, you can create an instance of Person,

03:13 and you can try printing this person as a dict, and you can do the same thing for the car.

03:30 And now you can open your terminal to run this. Run the file, and see both dictionaries being printed on the screen. Once you’re happy with your code, move on to the next lesson where you’re going to learn how to identify a mixin class.

Become a Member to join the conversation.