LearnFrenzy
Sign in Take a free test

Inheritance and Polymorphism · Predict the Output · Question 4

The order of cooperative __init__ calls Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

What is the output of the following Python program?

main.py
class A:
    def __init__(self):
        print("A", end=" ")
class B(A):
    def __init__(self):
        print("B", end=" ")
        super().__init__()
class C(A):
    def __init__(self):
        print("C", end=" ")
        super().__init__()
class D(B, C):
    def __init__(self):
        print("D", end=" ")
        super().__init__()

D()
print()
  • A D B A C A
  • B D B A
  • C D B C A Correct
  • D D C B A
Correct Answer: Option C

super() in B continues with the next class in D's MRO, which is C – not A. So every __init__ runs once.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links