How does Python's super() work with multiple inheritance?

后端 未结 16 2189
孤街浪徒
孤街浪徒 2020-11-21 05:19

I\'m pretty much new in Python object oriented programming and I have trouble understanding the super() function (new style classes) especially when it comes to

16条回答
  •  难免孤独
    2020-11-21 05:27

    In learningpythonthehardway I learn something called super() an in-built function if not mistaken. Calling super() function can help the inheritance to pass through the parent and 'siblings' and help you to see clearer. I am still a beginner but I love to share my experience on using this super() in python2.7.

    If you have read through the comments in this page, you will hear of Method Resolution Order (MRO), the method being the function you wrote, MRO will be using Depth-First-Left-to-Right scheme to search and run. You can do more research on that.

    By adding super() function

    super(First, self).__init__() #example for class First.
    

    You can connect multiple instances and 'families' with super(), by adding in each and everyone in them. And it will execute the methods, go through them and make sure you didn't miss out! However, adding them before or after does make a difference you will know if you have done the learningpythonthehardway exercise 44. Let the fun begins!!

    Taking example below, you can copy & paste and try run it:

    class First(object):
        def __init__(self):
    
            print("first")
    
    class Second(First):
        def __init__(self):
            print("second (before)")
            super(Second, self).__init__()
            print("second (after)")
    
    class Third(First):
        def __init__(self):
            print("third (before)")
            super(Third, self).__init__()
            print("third (after)")
    
    
    class Fourth(First):
        def __init__(self):
            print("fourth (before)")
            super(Fourth, self).__init__()
            print("fourth (after)")
    
    
    class Fifth(Second, Third, Fourth):
        def __init__(self):
            print("fifth (before)")
            super(Fifth, self).__init__()
            print("fifth (after)")
    
    Fifth()
    

    How does it run? The instance of fifth() will goes like this. Each step goes from class to class where the super function added.

    1.) print("fifth (before)")
    2.) super()>[Second, Third, Fourth] (Left to right)
    3.) print("second (before)")
    4.) super()> First (First is the Parent which inherit from object)
    

    The parent was found and it will go continue to Third and Fourth!!

    5.) print("third (before)")
    6.) super()> First (Parent class)
    7.) print ("Fourth (before)")
    8.) super()> First (Parent class)
    

    Now all the classes with super() have been accessed! The parent class has been found and executed and now it continues to unbox the function in the inheritances to finished the codes.

    9.) print("first") (Parent)
    10.) print ("Fourth (after)") (Class Fourth un-box)
    11.) print("third (after)") (Class Third un-box)
    12.) print("second (after)") (Class Second un-box)
    13.) print("fifth (after)") (Class Fifth un-box)
    14.) Fifth() executed
    

    The outcome of the program above:

    fifth (before)
    second (before
    third (before)
    fourth (before)
    first
    fourth (after)
    third (after)
    second (after)
    fifth (after)
    

    For me by adding super() allows me to see clearer on how python would execute my coding and make sure the inheritance can access the method I intended.

提交回复
热议问题