Mixins in Python
Join the DZone community and get the full member experience.Join For Free
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
Published at DZone with permission of Christian Posta, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.