1. Single inheritance: A subclass inherits only one parent class

Define a Master class
class Master(object) :
    def __init__(self) :
        # attribute
        self.kongfu = "Recipe for ancient Pancake Fruit." 

    # instance method
    def make_cake(self) :
        print("Made a pancake according to <%s>..." % self.kongfu)


Prentice is a subclass and Master is its parent class.
class Prentice(Master) : 
    A subclass can inherit all the attributes and methods of its parent class, even if it does not have its own attributes and methods.
    pass                

# laoli = Master()
# print(laoli.kongfu)
# laoli.make_cake()

damao = Prentice()  Create a subclass instance object
print(damao.kongfu) Subclass objects can use the attributes of their parent class directly
damao.make_cake()   Subclass objects can use the methods of their parent class directly
Copy the code

Description:

  • Although a subclass does not define an __init__ method initialization attribute, nor does it define an instance method, the parent class does. So whenever you create a subclass object, the inherited __init__ method is executed by default

Conclusion:

  • When a subclass inherits, the parentheses () are the name of the parent class when the class is defined
  • Attributes and methods of the parent class are inherited from the child class

2. Multiple inheritance: Subclasses inherit from more than one parent class

class Master(object) :
    def __init__(self) :
        self.kongfu = "Recipe for ancient Pancake Fruit."  # Instance variables, attributes

    def make_cake(self) :                    # instance method, method
        print("[ancient method] according to <%s> made a pancake fruit..." % self.kongfu)

    def dayandai(self) :
        print("Master's big pipe...")

class School(object) :
    def __init__(self) :
        self.kongfu = "Modern pancake recipe"

    def make_cake(self) :
        print("[Hyundai] made a pancake according to <%s>..." % self.kongfu)

    def xiaoyandai(self) :
        print("The little school bong...")

# class Prentice(School, Master):
# pass

# damao = Prentice()
# print(damao.kongfu)
# damao.make_cake()
# damao.dayandai()
# damao.xiaoyandai()


class Prentice(Master, School) :  # multiple inheritance, inherit multiple parent classes (Master before)
    pass

damao = Prentice()
print(damao.kongfu) # execute the Master attribute
damao.make_cake() Execute the Master instance method

The magic attribute __mro__ of a subclass determines the order in which attributes and methods are found
print(Prentice.__mro__)

damao.dayandai() # No duplicate name is not affected
damao.xiaoyandai()
Copy the code

Description:

  • Multiple inheritance can inherit from multiple parent classes, as well as the attributes and methods of all parent classes
  • Note: If there are attributes and methods of the same name in multiple parent classes, the attributes and methods of the first parent class are used by default (based on the order of the class’s magic attribute MRO).
  • Properties and methods with different names in multiple parent classes have no effect.