00:00 One of the great things about having access to multiple inheritance is that we can utilize what are called mixins. A mixin is a class that provides methods to other classes, but it’s not considered a base class itself.
Let’s say you want to convert objects of certain types in your application to a dictionary representation. We could provide a
.to_dict() method in each class, but the implementation of this method—or the code inside of it—is going to be identical for each class. Instead, let’s create a mixin class that defines that method and then we’ll make some other classes inherit from the mixin so they gain access to the method.
This mixin class will be like a utility, and as such, I don’t think we should code it in any of our existing modules. Instead, I’ll create a new module called
representations, which will house this class.
It’s only going to expose one method called
.to_dict(), which will scan for all of the instance attributes in the object it’s being called on, and then return a dictionary where the keys are the attribute names and the values are the attribute values.
02:19 I’ll have a link down in the video notes if you’d like to learn more about this, but if you’re not familiar, just understand that this code will turn our instance attributes into a dictionary of attribute names and their associated values.
In fact, this
self.__dict__.items() is a method that every custom class inherits from the
object superclass, which returns a
__dict__.items object that basically contains all the instance attributes and their values.
I’m calling it
._represent() to indicate that this is a private method that should be used only within this class, notably by the
.to_dict() method above. This method will be passed on to the child classes of this mixin, but the underscore (
_) tells other developers not to use it outside this mixin class. It’s just a helper method, if you will.
Again, in the interest of not having this course be five hours long, I’m not going to walk through this line by line. But basically, this method will return the proper format of the passed-in value appropriate for a dictionary. This might be a string, or if the current object has a
.to_dict() method itself—because it inherited from the mixin too—then it’ll return the dictionary generated by its
which will just filter out any instance attributes that are marked as private. All right. That’s it for the mixin class. To clarify how this actually works, let me utilize it in two other classes. First, I’m going to move over to the
Employee class. Before I can use the mixin anywhere, I need to import it at the top.
Remember, even though this is an inheritance relationship, the whole is a relationship we normally use does not apply here. The
Employee is not an
AsDictionaryMixin—it simply inherits the
.to_dict() method for use with this
That’s it. I don’t want every instance attribute to be represented in the generated dictionary, and so I’m going to mark two attributes as private by appending an underscore (
_) at the beginning of their names. If you remember from before, the dictionary comprehension code filters out any instance attributes that are marked as private, so these two instance attributes will not appear in our generated dictionary.
Commenting out this code will allow us to temporarily disable it. This demo will utilize a bit of
json, so I’ll import that at the top. I have a video course on JSON if you’d like to learn more about it.
._payroll attributes we marked as private, and so they were filtered out of the dictionary representation. Also, notice that the address of each employee is a dictionary in and of itself.
08:29 As you can see, mixins are a handy way to utilize multiple inheritance. They allow for the same method to be reused in multiple classes, and because they only inherit methods and not attributes, we don’t run the risk of causing any problems.
Become a Member to join the conversation.