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
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.
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.
From abc.abstarctmethod
description:
Note: Unlike
Java
abstract methods, these abstract methods may have an implementation. This implementation can be called via thesuper()
mechanism from the class that overrides it. This could be useful as an end-point for a super-call in a framework that uses cooperative multiple-inheritance.
Therefore you need just put stub doing nothing instead of raising NotImlementedError
.