-1
class Parent01(object):
    def foo(self):
        print("Parent01")
        pass

class Parent02(object):
    def foo(self):
        print("Parent02")
        pass

class Child(Parent01,Parent02):
    def foo(self):
        print("Child")
        super(Parent01, self).foo()
    pass

c = Child()
c.foo()

Output:

Child
Parent02

Why here is the output Parent02?

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
  • 1
    Possible duplicate of [How does Python's super() work with multiple inheritance?](http://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance). Also [Python's Multiple Inheritance: Picking which super() to call](http://stackoverflow.com/q/14206015/4831822) – Steven Summers Feb 22 '17 at 15:19
  • Possible duplicate of [python3 - behaviour of super() on multi-inheritance](http://stackoverflow.com/questions/24166225/python3-behaviour-of-super-on-multi-inheritance) – languitar Feb 22 '17 at 15:20

1 Answers1

1

You're misusing super. You're supposed to name your own class, not a parent. And given this is Python 3, you don't even need to do that, a simple:

super().foo()

would work (as long as the first parameter to the function is a single argument, regardless of name; there are exceptions for when you're accepting self via *args, but that's rare, and only for complicated cases involving simulating a dict).

The reason it misbehaves as written is that you've told it explicitly you're doing super based in Parent01, not Child, so it scans the MRO (method resolution order) to find the next class after Parent01, which happens to be Parent02.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271