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 is the name of the operation launched by TUDA to restore and protect the Swarnamukhi River in Andhra Pradesh?
Vienna has been named the most liveable city for the third consecutive year in the recently released 'Global Liveability Index 2024.' Which organization...
When is the Indian Navy scheduled to commission the ASW-SWC Arnala ship?
What was the growth rate of India's gross tax revenue for the 1st Quarter of 2023 (April to July) compared to the same period in the previous year, acco...
Which of the following statement/statements is/are INCORRECT regarding the RBI's Digital Payments Index?
1. The RBI's Digital Payments Index incr...
What resolution of the UN General Assembly proclaimed 23 June as United Nations Public Service Day?
Which ancient university's spirit did PM Modi seek to revive during his Independence Day speech?
Pieces of camphor placed on water move about rapidly. This is because of -
What was the aim of the Regional Search and Rescue exercise conducted by the Indian Coast Guard in Kakinada, Andhra Pradesh?
I. To conduct a real...
 In which of the following year the Ministry of Labour and Employment formally launched the e-Shram portal for creating a National Database of Unorgani...