Creating abstract method
Very popular method to declare an abstract method in Python class is to use NotImplentedError exception:
class SomeWorker:
def do_work(self):
raise NotImplementedError
This method even has IDE support (I use PyCharm, so at least it supports it).
The only downside of this approach is that you get the error only after method call.
>>> w = MyWorker()
# it's ok
>>> w.do_work()
NotImplementedError
It would be much better to know about the problem right after class instantiation.
Python's abstract base classes are here to help
from abc import ABCMeta, abstractmethod
class SomeWorker(metaclass=ABCMeta):
@abstractmethod
def do_work(self):
pass
For now if you subclass SomeWorker and doesn't override do_work
- you will get an error right upon class instantiation
>>> w = MyWorker()
TypeError: Can't instantiate abstract class MyWorker with abstract methods do_work