Bird
0
0

How should you design SmartPhone to properly initialize both parents following best practices?

hard📝 Application Q15 of 15
Python - Multiple Inheritance and Method Resolution

You want to create a class SmartPhone that inherits features from Camera and Phone. Both parents have an __init__ method. How should you design SmartPhone to properly initialize both parents following best practices?

ADefine <code>SmartPhone.__init__</code> and call <code>super().__init__()</code> only once, relying on parents to use super() too
BDefine <code>SmartPhone.__init__</code> but leave it empty
CDo not define <code>__init__</code> in <code>SmartPhone</code>, parents will initialize automatically
DDefine <code>SmartPhone.__init__</code> and call <code>Camera.__init__(self)</code> and <code>Phone.__init__(self)</code> directly
Step-by-Step Solution
Solution:
  1. Step 1: Understand multiple inheritance initialization

    Both Camera and Phone have __init__. To initialize both properly, each class should call super().__init__() so the MRO chain is followed.
  2. Step 2: Apply best practice in SmartPhone

    Define SmartPhone.__init__ and call super().__init__() once. This triggers the chain of __init__ calls in parents via MRO.
  3. Final Answer:

    Define SmartPhone.__init__ and call super().__init__() only once, relying on parents to use super() too -> Option A
  4. Quick Check:

    Use super() chain for clean multiple inheritance init [OK]
Quick Trick: Call super().__init__() once; parents must do the same [OK]
Common Mistakes:
  • Calling parent __init__ methods directly
  • Not calling any __init__ in child
  • Assuming parents initialize automatically without super()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes