Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Exposing the Mixin Class Within the ABC
00:00
You just implemented an abstract base class Sequence, you defined two abstract methods, and then you got a mixin method for free, .__iter__() because you’re able to implement .__iter__() in terms of .__getitem__() and .__len__().
00:15 But why is it called a mixin method? Why are you saying that this is a mixin method that you’re getting for free? If you were learning that mixin classes are classes that provide orthogonal behavior that you can then plug into your non-mixin classes through inheritance.
00:33
And the reason is that having an abstract base class like Sequence and providing the .__iter__() as you see on the screen is more or less equivalent to having an extra mixin class that provides .__iter__(), assuming .__getitem__() and .__len__() are defined.
00:51
This means that before defining the .__iter__() in Sequence, you could define an IterMixin that doesn’t inherit from anything and just defines the .__iter__().
01:02
And now when defining the .__iter__(), you can actually cut the .__iter__() from the abstract base class Sequence and you can just paste it inside IterMixin.
01:13
Now .__iter__() in the IterMixin class by itself does nothing. The thing is, this implementation of .__iter__() only works if the class inheriting the mixin already provides .__getitem__() and the .__len__().
01:33
But wait, the abstract base class Sequence on line 14 provides both methods. So you can inherit the IterMixin in your abstract base class Sequence.
01:46
And this is why the dunder method .__iter__() in the previous lesson was being referred to as a mixin method because having the method in the abstract base class is equivalent to defining it outside in a dedicated mixin and then realizing, wait, the abstract base class Sequence can already make use of the mixin. And if you save this piece of code with the IterMixin, note how the .__iter__() was removed from the Sequence. You can add a call to the print() function just to see that you’re using your .__iter__() that’s coming from the mixin.
02:18
So let’s say something like, hey, from inside IterMixin.__iter__, you add this call to the print() function. And now if you open your terminal,
02:31
you can run the file sequence.py and you’re going to see the 10 values from your simple reimplementation of range() and the result from calling the function print() from within IterMixin.__iter__.
02:47 So that’s why when you’re using abstract base classes and you’re implementing methods at the expense of your abstract methods, you call them or you tend to call them mixin methods.
02:56 It’s methods you’re getting for free.
02:59 In the next lesson, you’re going to learn about the common pitfalls you should avoid when using mixin classes.
Become a Member to join the conversation.
