Method Resolution Order in case of Base Classes Having different init params

前端 未结 5 1175
刺人心
刺人心 2021-01-20 05:52

I am trying to understand MRO in Python. Although there are various posts here, I am not particularly getting what I want. Consider two classes A and B

5条回答
  •  旧时难觅i
    2021-01-20 06:05

    Thanks all for helping me understand MRO. Below is my complete Code together with output. I hope this will also help other's.

    class BaseClass(object):

        def __init__(self, *args, **kwargs):
            self.name = kwargs.get('name')
    
        def printName(self):
            print "I am called from BaseClass"
            print self.name
    
        def setName(self, givenName):
            print "I am called from BaseClass"
            self.name=givenName
    
        def CalledFromThirdGen(self):
            print "I am called from BaseClass and invoked from Third Generation Derived            Class"
    

    class FirstGenDerived(BaseClass):

        def __init__(self, *args, **kwargs):
            super(FirstGenDerived, self).__init__(*args, **kwargs)
            self.name = kwargs.get('name')
            self.FamilyName = kwargs.get('FamilyName')
    
        def printFullName(self):
            print "I am called from FirstDerivedClass"
            print self.name + ' ' + self.FamilyName
    
        def printName(self):
            print "I am called from FirstDerivedClass, although I was present in BaseClass"
            print "His Highness " + self.name + ' ' + self.FamilyName
    

    class SecondGenDerived(BaseClass):

        def __init__(self, *args, **kwargs):
            super(SecondGenDerived, self).__init__(*args, **kwargs)
            self.name = kwargs.get('name')
            self.middleName = kwargs.get('middleName')
            self.FamilyName = kwargs.get('FamilyName')
    
        def printWholeName(self):
            print "I am called from SecondDerivedClass"
            print self.name + ' ' + self.middleName + ' ' + self.FamilyName
    
        def printName(self):
            print "I am called from SecondDerivedClass, although I was present in BaseClass"
            print "Sir " + self.name + ' ' + self.middleName + ' ' + self.FamilyName
    

    class ThirdGenDerived(FirstGenDerived, SecondGenDerived):

        def __init__(self, *args, **kwargs):
            super(ThirdGenDerived, self).__init__(*args, **kwargs)
    

    if name == "main":

        print "Executing BaseClass"
        BaseClass(name='Robin').printName()
    
        print "Executing Instance of BaseClass with SetName \n"
        Instance = BaseClass()
        Instance.setName("Little John")
        Instance.printName()
        print "################################################\n"
    
    
        print "Executing FirstGenDerived with printName and printFullName\n"
        FirstGenDerived(name='Robin', FamilyName='Hood').printFullName()
        FirstGenDerived(name='Robin', FamilyName='Hood').printName()
        print "################################################\n"
    
    
        print "Executing FirstGenderived with instance\n"
        Instance2 = FirstGenDerived(name=None, FamilyName="Hood")
        Instance2.setName("Edwards")
        Instance2.printFullName()
        print "################################################\n"
    
        print "Executing SecondGenDerived with printName and printWholeName\n"
        SecondGenDerived(name='Robin', FamilyName='Hood', middleName='Williams').printWholeName()
        SecondGenDerived(name='Robin', FamilyName='Hood', middleName='Williams').printName()
        print "################################################\n"
    
        print "Executing ThirdGenDerived\n"
        ThirdGenDerived(name='Robin', FamilyName='Hood', middleName='Williams').CalledFromThirdGen()
        ThirdGenDerived(name='Robin', FamilyName='Hood', middleName='Williams').printName()
        print "################################################\n"
    

    Output: Executing BaseClass I am called from BaseClass Robin Executing Instance of BaseClass with SetName

    I am called from BaseClass I am called from BaseClass Little John

    Executing FirstGenDerived with printName and printFullName

    I am called from FirstDerivedClass Robin Hood I am called from FirstDerivedClass, although I was present in BaseClass His Highness Robin Hood

    Executing FirstGenderived with instance

    I am called from BaseClass I am called from FirstDerivedClass Edwards Hood

    Executing SecondGenDerived with printName and printWholeName

    I am called from SecondDerivedClass Robin Williams Hood I am called from SecondDerivedClass, although I was present in BaseClass Sir Robin Williams Hood

    Executing ThirdGenDerived

    I am called from BaseClass and invoked from Third Generation Derived Class I am called from FirstDerivedClass, although I was present in BaseClass His Highness Robin Hood

提交回复
热议问题