Here's a typical use of abstract base classes in python:
from abc import ABC
class MyBaseClass(ABC):
@abstractmethod
def __init__(self):
print("Here's some logic that I want all subclasses to have")
class ChildClass(MyBaseClass):
def __init__(self):
super().__init__()
print("Here's some additional logic that I want this subclass to have")
The behavior is: it's not possible to instantiate MyBaseClass, but it is possible to subclass it, and MyBaseClass includes logic inherited by all subclasses.
Here's another way to get this same behavior:
class MyBaseClass:
def __init__(self):
if type(self) == MyBaseClass:
raise NotImplementedError
print("Here's some logic that I want all subclasses to have")
class ChildClass(MyBaseClass):
def __init__(self):
super().__init__()
print("Here's some additional logic that I want this subclass to have")
As far as I can tell, this approach has the exact same behavior, has the same amount of code, is equally explicit, and uses only native Python. So what does ABC do that this approach can't handle?