Python Forum
Multiple inheritance - the right way ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple inheritance - the right way ?
#4
This is how you run through the MRO:

class Top(object):

    def __init__(self):
        print('In class Top.')

class Left(Top):

    def __init__(self):
        print('In class Left.')
        super(Left, self).__init__()

class Right(Top):

    def __init__(self):
        print('In class Right.')
        super(Right, self).__init__()

class Bottom(Left, Right):

    def __init__(self):
        print('In class Bottom.')
        super(Bottom, self).__init__()

b = Bottom()
Output:
In class Bottom. In class Left. In class Right. In class Top.
You can't do that with __class__.__bases__, because Bottom will call Left and Right, and both of them will call Top, so Top will be called twice.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
RE: Multiple inheritance - the right way ? - by ichabod801 - Feb-13-2019, 05:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiple Inheritance - Help pls! magnusbrigido 1 1,880 May-17-2019, 12:56 PM
Last Post: ichabod801
  Multiple Inheritance using super() Sagar 2 7,368 Sep-08-2017, 08:58 AM
Last Post: Sagar

Forum Jump:

User Panel Messages

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