Christian Posta — Software Blog

Become the best you can be at your profession. If you've stopped learning, you've given up.

Mixins in Python

In Python, mixins allow a programmer to package a set of clearly defined and cohesive methods into a unit that can then be used to add functionality to other classes by “mixing in” these methods. This programming style, when used correctly, can lead to well-organized code as well as improved readability. However, in python, there may be better approaches than to use mixins. As Michele Simionato points out in his two part series (part 1 and part 2), python has better constructs for providing the same advantages to using mixins without the drawbacks.

The drawbacks Michele points out include: class namespace pollution, methond-name collisions, accidentally overridden methods, unclear class hierarchy, and cognitive load to understand classes composed of many mixins. To learn more about mixins, see an overview from wikipedia, this post from StackOverflow, this article from Linux Journal and of course the Artima posts from Michele (part 1 and part 2). Michele advises to use this as an alternative: Overloading/Generic Functions

One Response to “Mixins in Python”


Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>