Bird
Raised Fist0

You want to extend a parent class method process_data to add timing logs before and after the parent's processing. Which approach is best?

hard🚀 Application Q8 of Q15
Python - Inheritance and Code Reuse
You want to extend a parent class method process_data to add timing logs before and after the parent's processing. Which approach is best?
ACopy parent's <code>process_data</code> code and add timing logs in child
BOverride <code>process_data</code> in child, call <code>super().process_data()</code> between timing logs
CCall parent's <code>process_data</code> outside the child class
DUse <code>super.process_data()</code> without parentheses in child
Step-by-Step Solution
Solution:
  1. Step 1: Understand method extension

    To add behavior before and after the parent's method, override it in the child.
  2. Step 2: Use super() correctly

    Call super().process_data() inside the child method between timing logs.
  3. Step 3: Avoid code duplication

    Copying parent's code duplicates logic and is error-prone.
  4. Final Answer:

    Override process_data in child, call super().process_data() between timing logs -> Option B
  5. Quick Check:

    Use super() inside overridden method to extend behavior [OK]
Quick Trick: Override method, call super() inside to extend with extra code [OK]
Common Mistakes:
MISTAKES
  • Copy-pasting parent method code
  • Calling super without parentheses
  • Calling parent's method outside child class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes