FrenchToast
FrenchToast

Reputation: 51

Multilevel abstraction with interface and inheritance in Python

I'm not exactly sure how to phrase this question, hence the strange title. I also have not been able to find any information on this after searching, so hopefully this isn't a duplicate and I'm just searching for the wrong words. Anyhow, here is the situation, I have an abstract base class with some methods in it, which is inherited by a class. I don't want to set one of the methods in this base class, as this class is meant to be inherited by other classes to provide the common functionality they all share. Something like:

class A(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def fun1(self):
        pass

   @abc.abstractmethod
    def fun2(self):
        pass

class B(A):
    def fun1(self):
        #do work here

    @abc.abstractmethod
    def fun2(self):  # Intent to have the final classes define this
        pass

class C(B):
    def fun2(self):
        # do work here    

class D(B):
    def fun2(self):
        # do work here    

I would like to keep the function as an ABC.meta to force implementation on the final children, but because there can be multiple types of class B in this case all inheriting from the interface, I want to keep the initial virtulization of the method at this root class, but have a way for class B to enforce that it's sub-classes must implement this. The code works just find if I don't add the abstract method to class B, but that is awkward since subclassess must implement the method and shouldn't have to look all the way up to the interface to figure out everything they need to implement. As written, it will error out because class B cannot declare the method as an abc.abstract. If I don't declare it as an abstract there is no way to enforce the child class has to implement the method.

I hope my convoluted way of writing this makes sense to someone out there...

Thanks!

Upvotes: 4

Views: 1303

Answers (1)

James
James

Reputation: 36623

You probably should not redefine fun2 as an abstract method in the concrete class B. You are creating a set of rules for your interface, but immediately violating them when you do that.

Instead, either define a mix-in class or an additional ABC that C and D can inherit.

class A(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def fun1(self):
        pass

class A2(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def fun2(self):
        pass

class B(A):
    def fun1(self):
        print('hello')

class B2(A2):
    def fun2(self):
        print('world')

class C(B, B2):
    pass

class D(B, B2):
    pass

Upvotes: 2

Related Questions