0
0
Pythonprogramming~10 mins

Diamond problem 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 B that inherits from class A.

Python
class A:
    def greet(self):
        print("Hello from A")

class B([1]):
    pass
Drag options to blanks, or click blank then click option'
Aobject
BB
CA
DC
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong class name in parentheses.
Forgetting to put parentheses after the class name.
2fill in blank
medium

Complete the code to call the greet method from class A inside class B.

Python
class A:
    def greet(self):
        print("Hello from A")

class B(A):
    def greet(self):
        [1].greet(self)
        print("Hello from B")
Drag options to blanks, or click blank then click option'
AA
Bsuper()
Cself
DB
Attempts:
3 left
💡 Hint
Common Mistakes
Using self.greet() which causes recursion.
Using super().greet() which is correct but not the answer here.
3fill in blank
hard

Fix the error in the multiple inheritance class C that inherits from B and A.

Python
class A:
    def greet(self):
        print("Hello from A")

class B(A):
    def greet(self):
        print("Hello from B")

class C([1]):
    pass
Drag options to blanks, or click blank then click option'
AB, A
BA, B
CC, B
DB
Attempts:
3 left
💡 Hint
Common Mistakes
Reversing the order of inheritance.
Inheriting from only one class.
4fill in blank
hard

Fill both blanks to complete the method in class C that calls greet using super().

Python
class C(B, A):
    def greet(self):
        [1]().greet()
        print("Hello from C")
Drag options to blanks, or click blank then click option'
Aself
Bsuper
CC
Dobject
Attempts:
3 left
💡 Hint
Common Mistakes
Using self.greet() which causes recursion.
Using class names instead of super().
5fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps class names to their greet method outputs if the class is a subclass of A.

Python
classes = [A, B, C]
result = {({BLANK_2}}.__name__: [2]().greet() for [1] in classes if issubclass([2], A))
Drag options to blanks, or click blank then click option'
A{
Bcls
Attempts:
3 left
💡 Hint
Common Mistakes
Using list brackets instead of curly braces.
Using the wrong variable name.
Not calling greet on an instance.