Bird
Raised Fist0

Find the error in this code snippet:

medium📝 Debug Q7 of Q15
Python - Multiple Inheritance and Method Resolution
Find the error in this code snippet:
class A:
    def greet(self):
        return "Hi from A"

class B:
    def greet(self):
        return "Hi from B"

class C(A, B):
    def greet(self):
        return super(B, self).greet()

c = C()
print(c.greet())
ANo error, code runs fine
BMissing parentheses in class definition
CClass C should inherit only from one class
DIncorrect use of super() with arguments
Step-by-Step Solution
Solution:
  1. Step 1: Understand super() usage

    super() with arguments expects the first argument to be a class in the MRO before the current class.
  2. Step 2: Analyze super(B, self) call

    Here, super(B, self) looks for method after B in MRO, but B is not the current class, causing incorrect behavior.
  3. Final Answer:

    Incorrect use of super() with arguments -> Option D
  4. Quick Check:

    super() arguments must be current class and instance [OK]
Quick Trick: Use super() without arguments or with current class and self [OK]
Common Mistakes:
MISTAKES
  • Passing wrong class to super()
  • Misunderstanding super() in multiple inheritance
  • Expecting super() to call specific parent method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes