Archive - Originally posted on "The Horse's Mouth" - 2010-04-14 07:17:48 - Graham Ellis
Mixins allow you to add ("Mix in") code - usually shared code - from one class or module into another. They're a great way of providing great "light weight" multiple inheritance - I wrote about that [here] recently, and provided an example in Ruby to show how it works.
Mixins can also be used in Python - see [full example]. The "trick" here is to define the code to be mixed in as complete new classes, and to use Python's dynamic object structure to add in the new class by updating its __bases__ member: pubtrans.__bases__ += (label,)
You may compare that example with mixins to a full multiple inheritance example in Python which uses the same classes - I provided that other full source [here] and wrote about it [here].