I understand why the code below throws and exception, and some ways avoid that exception, but I don't understand the intended way to use @abstractmethod in order to create an abstract interface.
My goal is to
- create an abstract interface Foo that has a single argument constructor
- and an adapter class FooAdapter that can be subclassed by classes intending to implement Foo.
The problem is that if I add in the appropriate calls to "super" in the constructor I will eventually call the abstractmethod and raise an exception.
- Fix #1. Dont add super. works but seems wrong since it could drop info needed by other classes, if mixed with other classes
- Fix #2. Dont add the signature for init in the interface. works, but seems wrong since the whole point of an interface is to DEFINE THE INTERFACE. and I would not be doing that, at least not for the constructor.
I feel like I am thinking about this wrong. What the pythonic way??
from abc import ABC, abstractmethod
class Foo(ABC):
@abstractmethod
def __init__(self, my_arg):
raise NotImplementedError
class FooAdapter(Foo):
def __init__(self, my_arg):
super().__init__()
FROM THE ACCEPTED ANSWER BELOW
You don't need to use NotImplementedError at all, instead just use 'pass'. The @abstractmethod mechanism will throw an exception if a subclass does not implement the indicated method.