The Mixin mode can also be seen as a combination mode, which combines the functions of multiple classes to generate a class without inheritance, the following describes how to use the Mixin design pattern to program Python: The Mixin pattern is a pattern that is frequently used in python. appropriate and reasonable applications can achieve code reuse, the purpose of reasonably organizing the code structure.
Python's Mixin mode can be implemented through multi-inheritance. for example, we can customize a simple data container with a nested structure:
class SimpleItemContainer(object): def __init__(self, id, item_containers): self.id = id self.data = {} for item in item_containers: self.data[item.id] = item
SimpleItemContainer uses the python built-in Dict to store data. however, to access the corresponding data, you have to directly call the dictionary, data cannot be accessed through exposed APIs as easily as native dictionaries. Of course, the complete Dictionary Interface can also be fully implemented from the beginning, but it is definitely not possible to use a set of similar custom containers. at this time, the UserDict built in python is used. dictMixin is a good method:
From UserDict import DictMixin
class BetterSimpleItemContainer(object, DictMixin): def __getitem__(self, id): return self.data[id] def __setitem__(self, id, value): self.data[id] = value def __delitem__(self, id): del self.data[id] def keys(self): return self.data.keys()
By implementing the smallest Dictionary Interface and inheriting DictMixin to implement the Mixin mode, we can easily get the complete native Dictionary behavior: The following table syntax, get, has_keys, iteritems, itervalues even has a series of methods and implementations such as iterable protocol implementation.
Many frameworks, such as Django and Django rest framework, generally use the Mixin mode. when defining APIs or viewset, you can use multiple inheritance methods to take some functions.
Of course, the Mixin mode cannot be abused either. at least it will pollute your newly defined classes, and sometimes it will bring MRO problems; however, it is a good choice to put some basic and single functions such as the functions that are generally implemented through interface/protocol into the Mixin module:
class CommonEqualityMixin(object): def __eq__(self, other): return (isinstance(other, self.__class__) and self.__dict__ == other.__dict__) def __ne__(self, other): return not self.__eq__(other)class Foo(CommonEqualityMixin): def __init__(self, item): self.item = item
In fact, the whole understanding is nothing more than getting more functions through a combination, a bit like the interface in C # and java, emphasizing the meaning of "it can, however, it is much simpler than that, and there is no need to display constraints, and the mixin module comes with implementation. When using mixin, it seems that the class of mixin is placed on the right of the parent class to emphasize that this is not a typical multi-inheritance, but a special multi-inheritance, instead, on the basis of inheriting a base class, we can use the multi-inheritance function to add additional features to this subclass. Ensure that the Mixin class functions are single and specific. after mixing in, the MRO tree of the new class will be relatively simple and will not cause confusion.