0
0
Pythonprogramming~10 mins

Best practices for multiple inheritance in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a class that inherits from two parent classes.

Python
class Child([1]):
    pass
Drag options to blanks, or click blank then click option'
AParent1, Parent2
BParent1 Parent2
CParent1 & Parent2
DParent1 | Parent2
Attempts:
3 left
💡 Hint
Common Mistakes
Using spaces instead of commas between parent classes.
Using operators like & or | which are invalid here.
2fill in blank
medium

Complete the code to call the __init__ method of the first parent class in multiple inheritance.

Python
class Child(Parent1, Parent2):
    def __init__(self):
        [1].__init__(self)
Drag options to blanks, or click blank then click option'
Asuper()
BParent1
CChild
DParent2
Attempts:
3 left
💡 Hint
Common Mistakes
Using super() when wanting to call a specific parent's __init__.
Calling __init__ on the child class itself.
3fill in blank
hard

Fix the error in the code to properly use super() in multiple inheritance.

Python
class Child(Parent1, Parent2):
    def __init__(self):
        [1]().__init__()
Drag options to blanks, or click blank then click option'
Aself
BParent1
CChild
Dsuper
Attempts:
3 left
💡 Hint
Common Mistakes
Calling Parent1() instead of super().
Calling __init__ on self directly.
4fill in blank
hard

Fill both blanks to create a method that calls the same method from all parent classes using super().

Python
class Child(Parent1, Parent2):
    def greet(self):
        [1]().[2]()
Drag options to blanks, or click blank then click option'
Asuper
Bgreet
Chello
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Using self.greet() which causes recursion.
Using a wrong method name like hello.
5fill in blank
hard

Fill all three blanks to create a class that uses super() in __init__ and a method to demonstrate method resolution order.

Python
class Child([1]):
    def __init__(self):
        [2]().__init__()
    def show(self):
        print('Child show')
        [3]().show()
Drag options to blanks, or click blank then click option'
AParent1, Parent2
Bsuper
DParent2
Attempts:
3 left
💡 Hint
Common Mistakes
Not listing both parents in the class definition.
Calling parent methods directly instead of using super().