About Python's mixin mode

Source: Internet
Author: User

transferred from: http://www.bjhee.com/python-mixin.htmllanguages such as C or C + + support multiple inheritance, and a subclass can have more than one parent, a design that is often criticized. Because inheritance should be a "is-a" relationship. Cars, for example, inherit the vehicle class, because the sedan is a ("is-a") vehicle. An item cannot be many different things, so there should be no multiple inheritance. But is there a case where a class does need to inherit multiple classes?

The answer is yes, we still take the transportation to give example, the civil aircraft is a kind of transportation, for local tyrants The helicopter is also a kind of transportation. For both modes of transport, they all have a function of flying, but the sedan does not. Therefore, we can not write the flight function in the vehicle of the parent class. But if both civil aircraft and helicopters write their own flight methods, they violate the principle of reusing the code as much as possible (if there are more flying tools later, there will be a lot of duplicate code). What to do, then we have to let the two planes inherit the vehicle and the aircraft two of the parent class, so there are multiple inheritance. At this time also violated the inheritance must be "is-a" relationship. How can this puzzle be broken?

Different languages give a different approach, let's look at Java first. Java provides interface interface functionality to implement multiple inheritance:

123456789101112131415161718192021222324 public abstract class Vehicle { }public interface flyable { Public void fly(); } public class Flyableimpl implements flyable { Public void fly() { System. Out. println("I am Flying");     }} public class airplane extends Vehicle implements flyable { Private flyable; Public airplane() { flyable = new Flyableimpl();     } Public void fly() { flyable. Fly();     }}

Now our aircraft has both the vehicle and the vehicle properties, and we do not need to rewrite the flight methods in the aircraft, and we do not undermine the principle of single inheritance. Aircraft is a means of transport, the ability to fly is the property of the aircraft, through the inheritance interface to obtain.

Back to the topic, the Python language can have no interface functionality, but it can inherit multiple. Is Python supposed to be implemented with multiple inheritance? Yes, it's not. That is, because syntactically, it is true through multiple inheritance. Say no, because its inheritance still abide by "is-a" relationship, from the meaning of still follow the principle of single inheritance. How does that make sense? Let's look at an example.

123456789 class Vehicle(object): Pass class planemixin(object): def Fly(self): print ' I am Flying ' class airplane(Vehicle, planemixin): Pass

As you can see, the airplane class above implements multiple inheritance, but the second class it inherits is named Planemixin, not plane, which does not affect functionality, but tells the person who later reads the code, which is a mixin class. So from the meaning of understanding, airplane is just a vehicle, not a plane. This mixin, which means mixed (mix-in), tells others that this class is added as a function to a subclass, not as a parent class, and it functions as an interface in Java.

Use the Mixin class to implement multiple inheritance with great care

    • First it must represent a function, not an item, as Runnable,callable in Java.
    • Second it must be a single responsibility, if there are multiple functions, then write multiple Mixin class
    • Then, it does not depend on the implementation of the subclass
    • Finally, subclasses can still work even if they do not inherit the Mixin class, which is missing a function. (such as the aircraft can still carry passengers, just can't fly ^_^)

In addition, Reactjs also has a mixin function, and the syntax is simple:

1234567891011121314 var planemixin = function() { return { Fly: function() { console. Log(' I am Flying ');     }  }}var airplanecomponent = React. Createclass({ mixins: [planemixin()], render: function() { return ' ;   }});

About Python's mixin mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.