Question

Let's say I want to create SomeClass, which inherits from two classes:

class SomeClass(InheritedClass1, InheritedClass2):

Both the InheritedClass1 and InheritedClass2 have the method with the same name, named performLogic.

If I declare super().peformLogic(), I will get result only from the first argument/inherited class. I need the results of both, so my question is, is there a way to call the method from the InheritedClass1 and then from the InheritedClass2 by using super()?

Thanks.

EDIT:

Class example which I need to 'solve' is constructed like this (simplified, and skipped non-essential methods for brevity):

class One:
    ...

    def getOutput(self):
        self.output = self.performLogic()
        return self.output

class Two(One):
    ...

    def getFirstValue(self):
        return input()

    def getSecondValue(self):
        return input()

class Three(Two):
    ...

    def performLogic(self):
        (some logic performation based on inputs from class Two methods)

class Four(Two):
    ...

    def performLogic(self):
        (some *different* logic performation based on inputs from class Two methods)

What I need to do now is implement a class which will perform logic of both class Three as well as class Four but with only one pair of input values. So I declared:

class Five(Three,Four):
    def performLogic(self):
        *and here I got stuck*
        *super().performLogic() will ask me for input values and returns the
        *result of class Three's performLogic()*
        *but what of class Four, I need the result of it's performLogic() with
        *a single pair of input values, too?*
Was it helpful?

Solution

super is not a universal replacement for calling a method in a parent base class; it requires that classes be designed cooperatively. This means that every class needs to call super().performLogic, just in case it is not the last element of some class's MRO. Ultimately, there has to be some class at the end of the method resolution order which cannot call super().peformLogic(), either because it is the last class on the list or the next call would be delegated to a class (like object) which does not define performLogic. In this case, you'll have to provide such a root class yourself.

class LogicPerformer:
    def performLogic(self):
        # No call to super; the buck stops here, because object
        # doesn't have this method
        print("In LogicPerformer")

class InheritedClass1(LogicPerformer):

    def performLogic(self):
        print("In InheritedClass1")
        super().performLogic()

class InheritedClass2(LogicPerformer):

    def performLogic(self):
        print("In InheritedClass1")
        super().performLogic()

class SomeClass(InheritedClass1, InheritedClass2):

    def performLogic(self):
        print("In SomeClass")
        super().performLogic()

a = SomeClass()
print(SomeClass.__mro__)
a.performLogic()

OTHER TIPS

This is actually a very interesting question. I think there would not be any features in the language to allow this. What you basically want to do is to use method resolution in the language to call two methods where method resolution would always resolve one method. Hence, this cannot be done. If you want to call two separate methods, you need to do it yourself explicitly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top