Home python Multiple Inheritance in Python

Multiple Inheritance in Python

Author

Date

Category

I have this class of classes

class C:
  Def __init __ (Self):
    PRINT ('C')
Class A:
  Def __init __ (Self):
    PRINT ('A')
Class B (C):
  Def __init __ (Self):
    C .__ INIT __ (SELF)
    PRINT ('B')
Class D (B, A):
  x = 'x'

Create an object:

D ()

I get to my great surprise:

c

b

I thought all my life, which should be so until I came across:

c

b

a

Have you really need a second-parent class to initialize and call it constructor? Why so?


Answer 1, Authority 100%

When the class object is created, the interpreter tries to find the __ init __ method in accordance with the order of resolution of the methods (MRO) moving along the inheritance hierarchy until the first ancestor finds, With which this method is defined, and further to search will not be.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions