Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 - How does one define an abstract subclass from an existing abstract class?

Tags:

I initially defined the following abstract class:

from abc import ABC, abstractmethod    
class Primitive(ABC):

Now I want to create another abstract class that inherits from Primitive:

class InstrumentName(Primitive)

I need this class to be abstract since I ultimately want to create the following two concrete classes:

class CurrencyInstrumentName(InstrumentName)
class MetalInstrumentName(InstrumentName)

I have read the documentation and searched SO, but they mostly pertain to sublcassing concrete classes from abstract classes, or discussing how Python handles abstraction

like image 218
Abhay Nainan Avatar asked Feb 15 '18 18:02

Abhay Nainan


People also ask

Can an abstract class inherit another abstract class Python?

A class that is derived from an abstract class cannot be instantiated unless all of its abstract methods are overridden.

Can an abstract class be a subclass Python?

An abstract method is a method that is declared, but contains no implementation. Abstract classes cannot be instantiated, and require subclasses to provide implementations for the abstract methods.

Can we define subclass as abstract?

A subclass can be abstract even if its superclass is concrete. For example, the Object class is concrete, but may have an abstract subclass like GeometricObject . A subclass may override a method from its superclass to declare it abstract.

How do you define an abstract variable in Python?

To define an abstract method we use the @abstractmethod decorator of the abc module. It tells Python that the declared method is abstract and should be overridden in the child classes. We just need to put this decorator over any function we want to make abstract and the abc module takes care of the rest.


1 Answers

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.

like image 68
Martijn Pieters Avatar answered Sep 18 '22 08:09

Martijn Pieters