Design pattern Mixin is often used in python. There are two main situations where mixins are used 1:
- You want to provide a lot of optional features for a class.
- You want to use one particular feature in a lot of different classes.
Order of mixins definition
Order in which you use mixins defines the behaviour. Quote from 2:
in Python the class hierarchy is defined right to left, so in this case the Mixin2 class is the base class, extended by Mixin1 and finally by BaseClass. This is usually fine because many times the mixin classes don’t override each other’s, or the base class’ methods. But if you do override methods or properties in your mixins this can lead to unexpected results because the priority of how methods are resolved is from left to right.
|
|
Now this code
|
|
prints
BaseClass
If you change the order of mixins:
|
|
you get
Mixin2