Bird
Raised Fist0
Pythonprogramming~10 mins

Multiple inheritance syntax in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Multiple inheritance syntax
Define Parent1 class
Define Parent2 class
Define Child class(Parent1, Parent2)
Create Child object
Access methods/attributes from Parent1 and Parent2
Use Child object
Multiple inheritance means a class can inherit from more than one parent class, gaining their methods and properties.
Execution Sample
Python
class Parent1:
    def greet(self):
        return "Hello from Parent1"

class Parent2:
    def greet(self):
        return "Hello from Parent2"

class Child(Parent1, Parent2):
    pass

c = Child()
print(c.greet())
This code shows a Child class inheriting from two parents and calling the greet method.
Execution Table
StepActionEvaluationResult
1Define class Parent1 with greet methodNo outputParent1 class created
2Define class Parent2 with greet methodNo outputParent2 class created
3Define class Child inheriting Parent1 and Parent2No outputChild class created with multiple inheritance
4Create object c of class ChildNo outputObject c created
5Call c.greet()Look for greet in Child, then Parent1, then Parent2"Hello from Parent1"
6Print output of c.greet()Output to consoleHello from Parent1
💡 Execution stops after printing the greeting from Parent1 due to method resolution order.
Variable Tracker
VariableStartAfter CreationFinal
cundefinedChild object createdChild object with access to Parent1 and Parent2 methods
Key Moments - 2 Insights
Why does c.greet() call Parent1's greet and not Parent2's?
Because Python uses method resolution order (MRO) which checks Parent1 first as it is listed first in Child(Parent1, Parent2). See execution_table step 5.
Can Child override greet method?
Yes, Child can define its own greet method which will be called instead of parents'. This is not shown here but is possible.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed at step 6?
AError: ambiguous method
B"Hello from Parent2"
C"Hello from Parent1"
D"Hello from Child"
💡 Hint
Check step 5 and 6 in execution_table where c.greet() returns Parent1's greeting.
At which step is the Child object created?
AStep 4
BStep 3
CStep 5
DStep 2
💡 Hint
Look at execution_table step 4 where object c is created.
If Parent1 was listed after Parent2 in Child's definition, which greet would be called?
AParent1's greet
BParent2's greet
CChild's greet
DError due to conflict
💡 Hint
Method resolution order checks parents in the order they are listed in class definition.
Concept Snapshot
Multiple inheritance syntax in Python:
class Child(Parent1, Parent2):
    pass

Child inherits methods from both parents.
Method resolution order (MRO) decides which parent's method is used.
Order matters: first parent checked first.
Child can override any inherited method.
Full Transcript
This visual execution shows how Python handles multiple inheritance syntax. First, two parent classes Parent1 and Parent2 are defined, each with a greet method. Then, a Child class inherits from both parents using class Child(Parent1, Parent2). When we create an object c of Child and call c.greet(), Python looks for greet in Child first, then Parent1, then Parent2. Since Child has no greet, it uses Parent1's greet because Parent1 is listed first. The output is 'Hello from Parent1'. This example helps understand how multiple inheritance works and how Python decides which method to use when parents have methods with the same name.

Practice

(1/5)
1. What is the correct way to declare a class Child that inherits from two parent classes Parent1 and Parent2 in Python?
easy
A. class Child(Parent1, Parent2):
B. class Child(Parent1 & Parent2):
C. class Child inherits Parent1, Parent2:
D. class Child: Parent1, Parent2

Solution

  1. Step 1: Understand Python class inheritance syntax

    In Python, to inherit from multiple classes, list them separated by commas inside parentheses after the class name.
  2. Step 2: Match the syntax to the options

    class Child(Parent1, Parent2): uses the correct syntax: class Child(Parent1, Parent2):. Other options use invalid syntax.
  3. Final Answer:

    class Child(Parent1, Parent2): -> Option A
  4. Quick Check:

    Multiple inheritance syntax = class Child(Parent1, Parent2): [OK]
Hint: List parent classes separated by commas in parentheses [OK]
Common Mistakes:
  • Using '&' instead of commas between parent classes
  • Writing 'inherits' keyword like other languages
  • Not using parentheses after class name
2. Which of the following is a syntax error when defining a class with multiple inheritance?
easy
A. class MyClass(): pass
B. class MyClass(Parent1, Parent2): pass
C. class MyClass(Parent1): pass
D. class MyClass(Parent1 Parent2): pass

Solution

  1. Step 1: Check syntax for multiple inheritance

    Parent classes must be separated by commas inside parentheses.
  2. Step 2: Identify the incorrect option

    class MyClass(Parent1 Parent2): pass misses the comma between Parent1 and Parent2, causing a syntax error.
  3. Final Answer:

    class MyClass(Parent1 Parent2): pass -> Option D
  4. Quick Check:

    Missing comma between parents = SyntaxError [OK]
Hint: Always separate parent classes with commas [OK]
Common Mistakes:
  • Omitting commas between parent classes
  • Leaving out parentheses entirely
  • Using colons instead of commas
3. What will be the output of this code?
class A:
    def greet(self):
        return "Hello from A"

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

class C(A, B):
    pass

obj = C()
print(obj.greet())
medium
A. "Hello from A"
B. TypeError
C. AttributeError
D. "Hello from B"

Solution

  1. Step 1: Understand method resolution order (MRO)

    Class C inherits from A first, then B. Python looks for methods in the order of parents listed.
  2. Step 2: Determine which greet() is called

    Since A is first, C uses A's greet method, returning "Hello from A".
  3. Final Answer:

    "Hello from A" -> Option A
  4. Quick Check:

    MRO follows parent order = "Hello from A" [OK]
Hint: First parent class method is used in multiple inheritance [OK]
Common Mistakes:
  • Assuming last parent class method is called
  • Expecting an error due to multiple parents
  • Confusing method names or forgetting MRO
4. Find the error in this multiple inheritance code:
class X:
    pass

class Y:
    pass

class Z(X Y):
    pass
medium
A. Missing colon after class name
B. Missing comma between parent classes
C. Parent classes must be in square brackets
D. Class Z cannot inherit from X and Y

Solution

  1. Step 1: Check syntax for multiple inheritance

    Parent classes must be separated by commas inside parentheses.
  2. Step 2: Identify the syntax error

    In class Z(X Y):, the comma between X and Y is missing, causing a syntax error.
  3. Final Answer:

    Missing comma between parent classes -> Option B
  4. Quick Check:

    Comma missing between parents = SyntaxError [OK]
Hint: Separate parent classes with commas inside parentheses [OK]
Common Mistakes:
  • Forgetting commas between parent classes
  • Using square brackets instead of parentheses
  • Thinking multiple inheritance is not allowed
5. Given these classes:
class Alpha:
    def action(self):
        return "Alpha"

class Beta:
    def action(self):
        return "Beta"

class Gamma(Alpha, Beta):
    def action(self):
        return super().action() + " & Gamma"

class Delta(Gamma, Beta):
    pass

obj = Delta()
print(obj.action())

What is the output?
hard
A. "Beta & Gamma"
B. "Gamma"
C. "Alpha & Gamma"
D. AttributeError

Solution

  1. Step 1: Understand the inheritance chain and MRO

    Delta inherits from Gamma and Beta. Gamma inherits from Alpha and Beta. The MRO for Delta is Delta, Gamma, Alpha, Beta, object.
  2. Step 2: Trace the action() method call

    Delta uses Gamma's action(), which calls super().action(). In Gamma, super() refers to Alpha (next in MRO), so Alpha.action() returns "Alpha". Then Gamma appends " & Gamma".
  3. Final Answer:

    "Alpha & Gamma" -> Option C
  4. Quick Check:

    super() follows MRO = "Alpha & Gamma" [OK]
Hint: super() calls next in MRO, not just first parent [OK]
Common Mistakes:
  • Assuming super() calls Beta's method instead of Alpha's
  • Ignoring MRO order in multiple inheritance
  • Expecting an error due to complex inheritance