Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Mixins | Polymorphism and Abstraction
In-Depth Python OOP
course content

Зміст курсу

In-Depth Python OOP

Mixins

Mixins are exceptional examples of polymorphism, as they allow for altering the behavior and enhancing the functionality of another class. They are not meant to stand alone and lack a distinct logical context.

Extra functionality can either replace the base functionality or be supplementary to it.

Let's consider an example:

Each check_access() method can have a different implementation.

Mixins allow responsibility to change functionality in the classes.

Note

  • Mixin name should end with Mixin. This is the agreement of the programmers.
  • Mixins should be at the start of the inheritance order.
    class SomeClass(FirstMixin, LastMixin, FirstParent, LastParent)
  • You can use mixins to add new functionality in classes.
  • Mixins are considered dirty code because they use attributes and methods that are not specifically intended for themselves (as mixins are designed for other classes). Therefore, using mixins is generally considered a bad practice, and it is better to avoid them and use them only when necessary.

Все було зрозуміло?

Секція 4. Розділ 2
We're sorry to hear that something went wrong. What happened?
some-alt