31

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

If so, how should I do this?

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
Squilliam
  • 313
  • 1
  • 3
  • 5
  • 2
    https://stackoverflow.com/questions/48814211/python3-how-does-one-define-an-abstract-subclass-from-an-existing-abstract-cla – Jerther Jul 25 '18 at 13:04

2 Answers2

24

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
luminousmen
  • 1,971
  • 1
  • 18
  • 24
  • 13
    OK. My IDE (PyCharm) was complaining that I hadn't provided implementations for all abstract methods from class A in class B. I suppose I should just ignore those warnings, or is there some way to force it to recognise that class B is another abstract class? – Squilliam Jan 20 '18 at 02:56
  • You need to provide implementations for ALL abstract methods if will you need to create an instance afterwards – luminousmen Jan 21 '18 at 13:59
  • 3
    Yes. I need to provide implementations for all abstract methods in class C since it is a concrete class. However, I want to code class B in such a way that IDEs understand that it is an abstract class (just as IDEs understand inheritance with interfaces in Java). If it isn't possible, I may as well just ignore the warning. – Squilliam Jan 21 '18 at 22:06
  • 3
    In other words, my IDE shows a warning for class B in your code whilst it doesn't for class C. – Squilliam Jan 21 '18 at 22:12
  • Don't understand that behavior of ur IDE. I'm on VS Code now and do not see any warnings. The code above work for me like a charm – luminousmen Jan 22 '18 at 08:56
  • The warnings would have come from `pylint` or another linter. As far as I'm aware, the only way to get rid of the warning is to copy all the abstract methods for every class that is intended to be abstract. However, it's probably better to just disable that particular warning. – amin_nejad Jun 22 '20 at 17:48
  • 2
    You don't need to copy all the abstract methods for every class you just need to add the ABC inheritance to all subclasses that need to be abstract, for example: class Foo(ABC), class Bar(Foo, ABC), class Baz(Bar) Foo and Bar are abstract, Baz is not – fuzzKitty Apr 01 '21 at 10:21
  • You can do the following: `InheritedAbstractclass(OriginalAbstractclass, metaclass=ABCMeta): ...` – dbudaghyan Nov 14 '21 at 11:40
7
from abc import ABCMeta, abstractmethod

class OriginalAbstractclass(metaclass=ABCMeta):
    @abstractmethod
    def sample_method(self):
        pass


class InheritedAbstractClass(OriginalAbstractclass, metaclass=ABCMeta):
    @abstractmethod
    def another_method(self):
        pass

class ConcreteClass(InheritedAbstractClass):
    def some_method(self):
        pass

    def another_method(self):
        pass
dbudaghyan
  • 133
  • 2
  • 9