0
class Class2:
    def m(self):
        print("In Class2")
 
class Class3:
    def m(self):
        print("In Class3")
 
class Class4(Class2, Class3):
    def m(self):
        print("In Class4")  
        super().m()
        
      
obj = Class4()
obj.m()

Output:

In Class4
In Class2

My question is why I'm not seeing output

In Class4
In Class2
In Class3

even though I used multiple inheritance?

ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
  • The method `m()` is only called twice, so you only see two outputs. – quamrana Sep 19 '21 at 15:00
  • @quamrana: yes, and that's why they are asking the question. And there are three `m()` methods, only 2 are called (one calls another, and the 3rd one is left out because there is nothing to call it). Saying that *The method `m()` is only called twice* is confusing and not technically acurate. – Martijn Pieters Sep 19 '21 at 15:04
  • The exhaustive documentation of how Python orders class method lookups is https://www.python.org/download/releases/2.3/mro/ – Martijn Pieters Sep 19 '21 at 15:05
  • You can get the desired output by calling the super method in Class2 which will call the method of Class3 as precedence of inherited class is from Left To Right in python – TheSohan Sep 19 '21 at 15:05
  • @MartijnPieters: Sorry, but what is not `technically accurate` about `m() is only called twice()`? I only see two call sites. – quamrana Sep 19 '21 at 15:07
  • @quamrana there are three different `m()` methods. Your sentence implies there is only one. – Martijn Pieters Sep 19 '21 at 15:12
  • Sorry, I made the assumption that it was easy to see that there are three implementations of `m()`, but from a call-site point of view, there is only one name: `m` and it is python which gets to decide which actual implementation is called at runtime. – quamrana Sep 19 '21 at 15:17
  • @quamrana: not really; it is still up to the programmer. `instance.m()` calls the first one found on `type(instance).__mro__`, and subsequent `super().m()` calls find the next class in the `type(instance).__mro__` sequence. But you can call `ClassName.m(instance)` directly too, or `super(instance, ClassName).m()` and a variety of more convoluted options exist that retrieve a reference to one of the function objects. But that's nitpicking. – Martijn Pieters Sep 19 '21 at 17:02

0 Answers0