0

I have a question regarding the multi-level inheritance in Python. Following classes are in my script.

class A():
  def __init__(self, x, y, z):
    self.x = x

class B():
  def __init__(self, x, y, z):
    self.x = x

class C(A):
  def __init__(self, x):
    super.__init__(x)
    self.x = x

class D(B,C):
  def __init__(self, x):
    super.__init__(x)
    self.x = x

I'm not sure if my question will be understandable but I will try to explain.

  • If class C inherits from class A the form in class C will be a rectangle.
  • If class C inherits from class B the form in class C will be a circle.
  • But if class D inherits class B (Circle class) first and then class C(A) it will be a circle form although class C still inherits from class A (in the script). The other way around D(C,B) it will be a rectangle form (looks like because C(A) will be initialized first)

Why does the multi-inheritance in class D "override" the parent from class C to be a circle instead of a rectangle form?

tobsec
  • 36
  • 4
  • 2
    What do you mean "rectangle" and "circle". Can you provide concrete examples to clarify your question? – juanpa.arrivillaga Apr 09 '20 at 20:21
  • 1
    Python uses an algorithm called C3 Linearization to compute what's called a Method Resolution Order, which is how it tells in what order it inherits from parent classes. This article explains that algorithm and walks through some examples: https://www.python.org/download/releases/2.3/mro/ – Patrick Haugh Apr 09 '20 at 20:25
  • It's about the game pong with help of the library pygame. class A and B inherits the Form for the ball from a base class. class A creates a rectangle shape and class B creates a circle shape. class C is the actual game ball and the form is decided by the inheritance of class A or B. If I create a new ball in class D that inherits from class B (circle shape) and class C (predefined with inherited rectangle shape in another file I can't change) I will get a circle shaped ball. I think I'll get this because class D inherits from class B first. – tobsec Apr 09 '20 at 20:39
  • 1
    https://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance#3277407 – Michael D. Apr 09 '20 at 21:48

0 Answers0