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()).
Which of the following is NOT an Equestrian discipline?
Who has recently become the first African woman to become the 'Secretary-General of the Commonwealth of Nations'?
Which of the following is the largest cell in the human body?
Consider the following statement about India’s first Regional Rapid Transit System(RRTS):
1. It was inaugurated by Prime Minister Narendra Modi...
Krishna River flows in which state/states of India?
The Green Revolution was instrumental in reducing India's dependence on imports of:Â
What is the maximum investment limit per senior citizen under the PM Vaya Vandana Yojana?
What is the balance of trade of a country?
 Why was the National Investment and Infrastructure Fund (NIIF) created?
Which of the following CANNOT be grouped under physiographic divisions of India?