Python Multiple inheritance

Source: Internet
Author: User

1 #class people: #经典类2 classpeople (object): #新式类3     def __init__(self,name,age):4Self.name=name5Self.age= Age6self.friends=[]7     defEat (self):8         Print("%s is eating."%self.name)9     defSleep (self):Ten         Print("%s is sleeping."%self.name) One classRelation (object): A     defmake_friends (self,obj): -         Print('%s is making friends with%s'%(self.name,obj.name)) - self.friends.append (obj) the obj.friends.append (self) -  - classMan (people,relation):#multiple inheritance, inheritance order from left to right -     def __init__(Self,name,age,money): +         #people.__init__ (self,name,age) -Super (man, self).__init__(Name,age)#new types of writing +self.property= Money A         Print('%s had%s Yuan when he was born'%(self.name,self.property)) at     defSleep (self):# Refactoring -         #people.sleep (self) #继承父类 - Super (Man,self). Sleep () -         Print('%s is dahaning'%self.name) - classWoman (Relation,People):

- #Multiple inheritance (constructors only need one), inheritance order from left to right, \
in #the first parent class has a constructor that does not execute the constructor of the second parent class, and if the first parent class does not have a constructor, it goes to the second parent class to find

- Pass

1 m1=man ('Alex', 33,10)2 w1=woman (' May ', +)34m1.make_friends (W1)5print  (m1.friends[0].name) 6 Print (W1.friends[0].name)

Both the man and the woman classes inherit the two parent classes of people and relation, but there are no constructors in the relation class. When woman is instantiated, it goes to the relation class to find the constructor, because it is not found, so go to the people class to find the constructor (left to right order).

About Inheritance Order:

In Python2, the new class is inherited by the breadth first, and the classic class is inherited by the depth first.
In Python3 , both the new class and the classic class are inherited by breadth precedence .

Python Multiple inheritance

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.