Python Forum
super() and order of running method in class inheritance
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
super() and order of running method in class inheritance
#1
hi in code:
'''
from:https://stackoverflow.com/questions/64740117/how-method-resolution-order-mro-is-working-in-this-python-code
title of the above page: How Method Resolution Order (MRO) is working in this Python code
'''

class parent:
    def __init__(self):
        self.a=2
        self.b=4
    def form1(self): 
        print("calling parent from1")
        print('p',self.a+self.b)
 
class child1(parent):
    def __init__(self):
        self.a=50
        self.b=4
    def form1(self):
        print('bye',self.a-self.b)
    def callchildform1(self):
        print("calling parent from child1")
        super().form1()
 
class child2(parent):
    def __init__(self):
        self.a=3
        self.b=4
    def form1(self):
        print('hi',self.a*self.b)
    def callchildform1(self):
        print("calling parent from child2")
        super().form1()
 
class grandchild(child1,child2):
    def __init__(self):
        self.a=10
        self.b=4
    def callingparent(self):
        super().form1()
        
print(f" grandchild.__mro__ is {grandchild.__mro__}")
g=grandchild()
g.callchildform1()
print(f"child1.__mro__ is {child1.__mro__}")
the output is:
Output:
grandchild.__mro__ is (<class '__main__.grandchild'>, <class '__main__.child1'>, <class '__main__.child2'>, <class '__main__.parent'>, <class 'object'>) calling parent from child1 hi 40 child1.__mro__ is (<class '__main__.child1'>, <class '__main__.parent'>, <class 'object'>)
according to MRO of the class grandchild, the line g.callchildform1() causes to callchildform1's method in classchild1 class is called and in this method there is super().form1() (line 22).
now we are in the class child1 and for the mro of this class, the form1 method of parent class must be called. but the form1 method of class child2 is called.
explain to me why Python acts as this.
As in the docstring of this code has been written, this subject has been discussed in the given address, but the issue is ambiguous to me still.
thanks
Reply


Messages In This Thread
super() and order of running method in class inheritance - by akbarza - Feb-01-2024, 07:21 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems with super() Hoespilaar 3 531 Jun-11-2024, 02:15 AM
Last Post: kanetracy
  class definition and problem with a method HerrAyas 2 437 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  super() in class akbarza 1 709 Dec-19-2023, 12:55 PM
Last Post: menator01
  the order of running code in a decorator function akbarza 2 670 Nov-10-2023, 08:09 AM
Last Post: akbarza
  "Name is not defined" when running a class lil_e 6 4,676 Jan-12-2023, 11:57 PM
Last Post: lil_e
  Using one child class method in another child class garynewport 5 1,847 Jan-11-2023, 06:07 PM
Last Post: garynewport
  Child class inheritance issue eakanathan 3 1,508 Apr-21-2022, 12:03 PM
Last Post: deanhystad
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,430 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,770 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 2,086 Feb-07-2022, 07:29 PM
Last Post: saavedra29

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020