Question
The following Python code intends to enforce that all
`Worker` subclasses implement a `work` method. However, it's not working as expected. class Worker: Â Â def work(self): Â Â Â Â raise NotImplementedError("Subclasses must implement this method") class Engineer(Worker): Â Â pass eng = Engineer() eng.work() What is the most Pythonic way to fix this to ensure `Engineer` *must* implement `work` at compile/definition time?Solution
o The original Worker class uses NotImplementedError, which is a runtime check. The error only occurs when eng.work() is actually called. o To enforce that Engineer must implement work at the time the Engineer class is defined (or instantiated), you need to use Python's Abstract Base Classes (ABCs). o By inheriting Worker from abc.ABC and decorating work with @abstractmethod, Worker becomes an abstract class. Any concrete subclass (like Engineer) that fails to implement work will raise a TypeError when you try to instantiate it (e.g., eng = Engineer()).
What was the age of veteran actor Dharmendra at the time of his passing?
Which of the following countries will host the 20th G 20 summit?
What is the approved project cost for the Smart and Integrated Fishing Harbour at Mayabunder in the Andaman and Nicobar Islands?
- Where did the World Para Athletics Grand Prix 2025 take place?
Which ministries have collaborated to promote and develop the Unani system of medicine in India?
What is the total corpus limit under NPS for which 100% withdrawal is now permitted?
What is the main objective of the MoU between CSIR and MSSRF which was in news recently?
Which of the following minerals recorded the highest percentage increase in production in February 2024 compared to February 2023?
As per Economic Survey 2025–26, what is the projected range for India’s real GDP growth for FY27?Â
What is the upper limit for limited tendering introduced in DPM 2025?