Overriding Methods in Mixins

suggest change

Mixins are a sort of class that is used to “mix in” extra properties and methods into a class. This is usually fine because many times the mixin classes don’t override each other’s methods, or the base class’s. But if you do override methods or properties in your mixins this can lead to unexpected results because method lookup follows the MRO from left to right.

For instance, take the following classes. Their definitions are not on this page.

In this case the Mixin2 class is the base class, extended by Mixin1 and finally by BaseClass. Thus, if we execute the following code snippet:

>>> x = MyClass()
>>> x.test()
Base

We see the result returned is from the Base class. This can lead to unexpected errors in the logic of your code and needs to be accounted for and kept in mind.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents