Bird
0
0

How should you design Service to ensure both parent initializers run properly without duplication?

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

You have three classes: Logger, Authenticator, and Service. You want Service to inherit from both Logger and Authenticator. How should you design Service to ensure both parent initializers run properly without duplication?

AUse <code>super().__init__()</code> in all classes and ensure they call super() in their <code>__init__</code>.
BCall each parent <code>__init__</code> directly inside <code>Service.__init__</code>.
COnly call <code>Logger.__init__</code> in <code>Service</code> and skip <code>Authenticator</code>.
DDo not define <code>__init__</code> in <code>Service</code> to avoid conflicts.
Step-by-Step Solution
Solution:
  1. Step 1: Understand multiple inheritance init calls

    To ensure all parent initializers run once, each class should call super().__init__() so Python follows MRO.
  2. Step 2: Design Service and parents accordingly

    All classes including Logger and Authenticator must use super().__init__() in their constructors.
  3. Final Answer:

    Use super().__init__() in all classes and ensure they call super() in their __init__. -> Option A
  4. Quick Check:

    Use super() in all __init__ to avoid duplication [OK]
Quick Trick: All __init__ should call super().__init__() for safe multiple inheritance [OK]
Common Mistakes:
  • Calling parent __init__ methods directly
  • Skipping __init__ in some parent classes
  • Assuming __init__ runs automatically for all parents

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes