1

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?*
2
  • 3
    see stackoverflow.com/questions/11354786/… Commented May 13, 2014 at 13:16
  • 1
    Thanks, but this is Python 2 example, and more of a 'understanding clarification', I need Python 3 syntax for this specific problem. Commented May 13, 2014 at 13:26

2 Answers 2

4

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()
Sign up to request clarification or add additional context in comments.

4 Comments

I've added an concrete example of class design which I need to 'extend'.
It's not entirely clear that inheritance is the right model for your code. You seem to be using it purely for control flow. Perhaps composition (where a Five contains references to Three and Four objects) is more appropriate.
Yes, but then Three object will ask for its own set of input values, as well as Four object, and I need to do it work with just one set of inputs. Perhaps more general design makeover is needed?
Yes. For example, notice that Two.getInput() neither reads from nor modifies the state of its instance; there's little reason for it to exist inside the class hierarchy. It would be better to prompt for input first, then pass the resulting values to Five.__init__, which could use Three and Four as necessary. It's possible you don't actually need any classes here, just suitably defined functions.
0

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.

3 Comments

He has the right use case for super; he just isn't using it correctly.
@chepner: not really. Wanting to call the same method from two different paths in the hierarchy chain is not really a good use-case for super. Calling a single resolved method however, is.
Ooh, comment retracted after seeing his actual use case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.