Search code examples
pythonclassoopabstractabc

How to make an Abstract Class inherit from another Abstract Class in Python?


Is it possible to have an Abstract Class inheriting from another Abstract Class in Python?

If so, how should I do this?


Solution

  • Have a look at abc module. For 2.7: link. For 3.6: link Simple example for you:

    from abc import ABC, abstractmethod
    
    class A(ABC):
        def __init__(self, value):
            self.value = value
            super().__init__()
    
        @abstractmethod
        def do_something(self):
            pass
    
    
    class B(A):
        @abstractmethod
        def do_something_else(self):
            pass
    
    class C(B):
        def do_something(self):
            pass
    
        def do_something_else(self):
            pass