Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

Look at the below code:

class Parent(object):

    def __init__(self, name, serial_number):

        self.name = name

        self.serial_number = serial_number

class ChildA(Parent):

    def __init__(self, name, serial_number):

        self.name = name

        self.serial_number = serial_number

        super(ChildA, self).__init__(name = self.name, serial_number = self.serial_number)

    def speak(self):

        print("I am from Child A")

class ChildB(Parent):

    def __init__(self, name, serial_number):

        self.name = name

        self.serial_number = serial_number

        super(ChildB, self).__init__(name = self.name, serial_number = self.serial_number)

    def speak(self):

        print("I am from Child B")

class GrandChild(ChildA, ChildB):

    def __init__(self, a_name, b_name, a_serial_number, b_serial_number):

        self.a_name = a_name

        self.b_name = b_name

        self.a_serial_number = a_serial_number

        self.b_serial_number = b_serial_number

        super(GrandChild, self).__init_( something )

When executing the super function in GrandChild, what is the appropriate method to design the __init__ arguments with the goal that ChildA and ChildB both get the right arguments? 

Likewise how would you access the two distinct versions of the speak method (ChildA's rendition and ChildB's adaptation) from inside the GrandChild class?

1 Answer

0 votes
by (26.4k points)

Along these lines, when you call super from the grandchild, ChildA's __init__ method will be called in light of the fact that super follows the __mro__ property (parents left to right then grandparents left-to-right, at that point distant grandparents, ...) 

Since ChildA's init likewise calls super, at that point all the super calls will be binded, calling child b's __init__ and ultimately the parent init. In circumstances where that is not the situation, keywords may work better.

In circumstances where that is not the situation, keywords arguments might work better.

class Parent:    

    def __init__(self, name, serial, **kwargs):

        self.name = name

        self.serial = serial

class ChildA(Parent):    

    def __init__(self, a_name, a_serial, **kwargs):

        self.a_name = a_name

        self.a_serial = a_serial

        super().__init__(**kwargs)

class ChildB(Parent):    

    def __init__(self, b_name, b_serial, **kwargs):

        self.b_name = b_name

        self.b_serial = b_serial

        super().__init__(**kwargs)

class GrandChild(ChildA, ChildB):

    def __init__(self):

        super().__init__(name = "blah", a_name = "a blah", b_name = "b blah", a_serial = 99, b_serial = 99, serial = 30)

Are you pretty much interested to learn python in detail? Come and join the python training course to gain more knowledge.

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 17, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...