ProgrammingVery hardProduct company OA

What is printed?

What is printed?
class Base:
    def __init__(self):
        self.log = ["Base"]
class L(Base):
    def __init__(self):
        super().__init__()
        self.log.append("L")
class R(Base):
    def __init__(self):
        super().__init__()
        self.log.append("R")
class X(L, R):
    def __init__(self):
        super(L, self).__init__()
        self.log.append("X")

print(X().log, [c.__name__ for c in X.__mro__][1:3])
  1. A. ['Base', 'R', 'X'] ['L', 'R']
  2. B. ['Base', 'L', 'X'] ['L', 'R']
  3. C. ['Base', 'R', 'L', 'X'] ['L', 'R']
  4. D. ['Base', 'X'] ['L', 'Base']

Correct answer: A. ['Base', 'R', 'X'] ['L', 'R']

X's MRO is X, L, R, Base, object. super(L, self) means 'the class after L in self's MRO', which is R, so L.__init__ is skipped entirely. R.__init__ calls super().__init__(), which in X's MRO is Base: log becomes ['Base'], then R appends 'R', then X appends 'X'. Plain super() in X would have run L too.

More Python Programming questions

Exams that ask this topic