Bird
0
0

Find the error in this code snippet:

medium📝 Debug Q7 of 15
Python - Polymorphism and Dynamic Behavior
Find the error in this code snippet:
from abc import ABC, abstractmethod

class Writer(ABC):
    @abstractmethod
    def write(self, text):
        pass

class FileWriter(Writer):
    def write(self):
        print("Writing to file")

fw = FileWriter()
AAttributeError when calling write method
BTypeError because FileWriter's write method signature does not match abstract method
CSyntaxError due to missing abstractmethod decorator
DNo error, FileWriter correctly implements write
Step-by-Step Solution
Solution:
  1. Step 1: Examine the method definitions

    Writer's abstract method 'write' requires one parameter 'text'. FileWriter's 'write' has no parameters.
  2. Step 2: Understand abstract method implementation rules

    Python's abc module requires that the subclass method matches the abstract method's signature. A mismatch causes TypeError on instantiation.
  3. Final Answer:

    TypeError because FileWriter's write method signature does not match abstract method -> Option B
  4. Quick Check:

    Method signature mismatch = TypeError on instantiation [OK]
Quick Trick: Method signatures must match abstract methods to avoid TypeError [OK]
Common Mistakes:
  • Thinking ABCs do not enforce method signatures
  • Expecting no error despite signature mismatch
  • Confusing instantiation success with runtime call errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes