Python3 - How does one define an abstract subclass from an existing abstract class?
Just subclass, you don't need to do anything special.
A class only becomes concrete when there are no more abstractmethod
and abstractproperty
objects left in the implementation.
Let's illustrate this:
from abc import ABC, abstractmethod
class Primitive(ABC):
@abstractmethod
def foo(self):
pass
@abstractmethod
def bar(self):
pass
class InstrumentName(Primitive):
def foo(self):
return 'Foo implementation'
Here, InstrumentName
is still abstract, because bar
is left as an abstractmethod
. You can't create an instance of that subclass:
>>> InstrumentName()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class InstrumentName with abstract methods bar
Subclasses can also add @abstractmethod
or @abstractproperty
methods as needed.
Under the hood, all subclasses inherit the ABCMeta
metaclass that enforces this, and it simply checks if there are any @abstractmethod
or @abstractproperty
attributes left on the class.