Search code examples
pythoninheritanceabc

Inherited Abstract Classes in python


In python, can I define an interface (abstract class) by inheritance from another abstract class? If I try:

import abc
ABC = abc.ABCMeta('ABC', (object,), {})

class interface(ABC):
    @abc.abstractmethod
    def method(self, message):
        return


class InterfaceExtended(ABC, interface):
    @abc.abstractmethod
    def NewMethod(self, message):
        return

I get an error on the "InterfaceExtended" class :

TypeError: Error when calling the metaclass bases
Cannot create a consistent method resolution
order (MRO) for bases ABC, Interface

Solution

  • Don't inherit from ABC in your second class. The interface it derives from already inherits ABC