Mixin introduction Mixin programming is a development mode that combines the functional units of multiple classes, it sounds like a class inheritance mechanism can be implemented, but this is different from the traditional class inheritance. Generally, mixin does not serve as the base class of any class, nor does it care about the class used together, but dynamically combines it with other scattered classes at runtime. Features the mixin mechanism has the following advantages: existing classes can be extended without modifying any source code; components can be classified; as needed, use existing functions for combination to implement the "new" class. This avoids the limitations of class inheritance, because new businesses may need to create new subclasses. Multiple inheritance Python supports multiple inheritance, that is, a class can inherit multiple subclasses. You can use this feature to conveniently implement mixin inheritance. The following code shows that Class A and Class B indicate different functional units. C is A combination of the functions of Class A and Class B, so class C has the functions of Class A and Class B. [Python] class A: def get_a (self): print 'A' class B: def get_ B (self): print 'B' class C (a, B ): pass c = C () c. get_a () c. the implementation of get_ B () _ bases _ multi-inheritance creates A new class. Sometimes, when we want to add the function of Class B to Class A during runtime, you can also use the metaprogramming feature of python to easily add class B features to Class A at runtime using the attribute __bases _. The code below is: [python]. _ bases _ + = (B,). get_ B () actually _ bases _ is also an inheritance mechanism, because the _ bases _ attribute stores the base class of the class. Therefore, multiple inheritance methods can also be implemented as follows: [python] class C: pass C. _ bases _ + = (A, B,) the plug-in method is based on the multi-inheritance and python metaprogramming features. However, when business needs change, if you need A new combination of functions, you need to re-Modify the base class of A. This will bring about synchronization problems because we are changing the features of the class, not the objects. Therefore, the above changes will affect all modules that reference this class, which is quite dangerous. We usually want to modify the behavior of an object, rather than modifying the class. Similarly, we can use _ dict _ to extend the object method. [Python] view plaincopyclass PlugIn (object): def _ init _ (self): self. _ exported_methods = [] def plugin (self, owner): for f in self. _ exported_methods: owner. _ dict _ [f. _ name _] = f def plugout (self, owner): for f in self. _ exported_methods: del owner. _ dict _ [f. _ name _] class AFeature (PlugIn): def _ init _ (self): super (AFeature, self ). _ init _ () self. _ exported_methods.append (self. get_a_value) def get_a_value (self): print 'a feature. 'class BFeature (PlugIn): def _ init _ (self): super (BFeature, self ). _ init _ () self. _ exported_methods.append (self. get_ B _value) def get_ B _value (self): print 'B feature. 'class Combine: pass c = Combine () AFeature (). plugin (c) BFeature (). plugin (c) c. get_a_value () c. get_ B _value ()