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
#3
Quote: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.
What part confuses you? Maybe an example with more depth will help?
class A:
pass

class B:
pass

class C:
pass

class D:
pass

class E(A, B):
pass

class F(C, D):
pass

class G(E, F):
pass

print(*G.__mro__, sep="\n")
Output:
<class '__main__.G'> <class '__main__.E'> <class '__main__.A'> <class '__main__.B'> <class '__main__.F'> <class '__main__.C'> <class '__main__.D'> <class 'object'>
Which we can draw like this:
Output:
A B C D \ / \ / E F \ / G
We traverse the mro graph giving preference the left node (first superclass). If we don't find what we need in the left branch, back up one level and try the next superclass. So if we cannot find the method we are looking for in A, back up to E and try the next superclass of E which is B. If we can't find it there we have to back up to G and check the next superclass, F. The search order is Look local, then look at leftmost super. When you reach object, back up and look at next super.

In practice, using inheritance like this will quickly drive you insane, and multiple inheritance examples in Python are rare compared to single inheritance. But the mechanism is in place for you to drive yourself insane if that's what you want to do.
Reply


Messages In This Thread
RE: super() and order of running method in class inheritance - by deanhystad - Feb-01-2024, 12:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems with super() Hoespilaar 3 540 Jun-11-2024, 02:15 AM
Last Post: kanetracy
  class definition and problem with a method HerrAyas 2 441 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  super() in class akbarza 1 721 Dec-19-2023, 12:55 PM
Last Post: menator01
  the order of running code in a decorator function akbarza 2 686 Nov-10-2023, 08:09 AM
Last Post: akbarza
  "Name is not defined" when running a class lil_e 6 4,690 Jan-12-2023, 11:57 PM
Last Post: lil_e
  Using one child class method in another child class garynewport 5 1,861 Jan-11-2023, 06:07 PM
Last Post: garynewport
  Child class inheritance issue eakanathan 3 1,510 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,437 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,775 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,099 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