Top-rated recipes tagged "mixins"http://code.activestate.com/recipes/tags/mixins/top/2011-08-12T15:29:21-07:00ActiveState Code RecipesMixin and Overlay (Python) 2011-06-01T05:16:47-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577730-mixin-and-overlay/ <p style="color: grey"> Python recipe 577730 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/mixins/">mixins</a>). </p> <h4 id="the-two-sides-of-classes">The Two Sides of Classes</h4> <p>Class inheritance either adds new attributes to a class, or changes existing attributes. However, this distinction is not made in Python's class system. This recipe is a stab at providing that separation. It takes advantage of metaclasses and class decorators to do it.</p> <p>For this recipe the two sides of class inheritance are called mixins and overlays. Mixins add new attributes to a class. Overlays change existing attributes, which in the case of methods means changing the behavior of the methods. However, overlays do not add any attributes.</p> <h4 id="separation-of-concerns">Separation of Concerns</h4> <p>Another issue with class inheritance is that abstraction rarely breaks down into perfect trees with a clear separation of concerns. Python allows multiple inheritance, which can help, but requires cooperation between classes in the diamond hierarchy. Mixins and overlays help with this problem. Hopefully, that will be evidenced by the recipe and subsequent examples.</p> <p>With this approach, the main single inheritance line can focus on the core abstraction and mixins/overlays can be used to extend the classes in other directions. This does not solve all the problems regarding separation of concerns, but it solves some. I hope to address the rest in another recipe that centers around delegation through a component architecture on instances.</p> <h4 id="interfaces">Interfaces</h4> <p>Since Python 2.6 we have had Abstract Base Classes providing a mechanism for promising what interfaces an object provides. I'll show in one of the examples how an ABC can be split into a interface portion and a mixin portion. The recipe takes advantage of the ABC functionality in the Python type system. I expect that mixins and overlays would be a good fit with one of my other recipes (<a href="http://code.activestate.com/recipes/577711/">Recipe 577711</a>).</p> <h4 id="the-recipe-classes">The Recipe Classes</h4> <p>This recipe provides a standard approach to applying mixin classes without using inheritance. This is done through a metaclass. The metaclass builds a __mixins__ attribute on the class and provides a mixes_in class decorator. Applying that decorator to the class will add the attributes in __mixins__ to the decorated class. However, if any of those names are already bound on the name then it will fail. The decorator returns the modified class.</p> <p><strong>Note</strong>: Traditional mixins are typically done through multiple inheritance, as opposed to class decorators.</p> <p>This recipe also provides a companion to mixin classes, called overlays. This is done through a metaclass in exactly the same way as the mixins, but provides an __overlays__ attribute and a class decorator called overlays. In contrast to mixins, if an overlay attribute is missing on the decorated class, it will fail. This is because the decorator will return a new class that inherits from the decorated class, and overrides the attributes in __overlays__.</p> <p><strong>Note</strong>: Metaclasses are used here because we need to pull from the class namespace there. A class decorator does not afford us the same functionality without more complexity.</p> <p><strong>Note</strong>: This recipe should work fine in 2.7 with a switch to the __metaclass__ syntax.</p> Mixins by Inheritance vs. by Decorator...Let's Try Decorators (Python) 2011-08-12T15:29:21-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577824-mixins-by-inheritance-vs-by-decoratorlets-try-deco/ <p style="color: grey"> Python recipe 577824 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/mixins/">mixins</a>). </p> <p>Using mixin classes via inheritance has its pros and cons. Here is an easy alternative via a decorator. As a bonus, you can mix in attributes from any object, not just classes.</p>