What is happening here in Python with OOPs while t

2019-08-10 14:48发布

问题:

I am learning OOPs with python. created below code to replicate the diamond shape problem in multiple inheritence. I am running the below code in jupyter notebook and output is generated at same.

class parent:
    def __init__(self):
        self.a=2
        self.b=4
    def form1(self): 
        print("calling parent from1")
        print('p',self.a+self.b)

class child1(parent):
    def __init__(self):
        self.a=50
        self.b=4
    def form1(self):
        print('bye',self.a-self.b)
    def callchildform1(self):
        print("calling parent from child1")
        super().form1()

class child2(parent):
    def __init__(self):
        self.a=3
        self.b=4
    def form1(self):
        print('hi',self.a*self.b)
    def callchildform1(self):
        print("calling parent from child2")
        super().form1()

class grandchild(child1,child2):
    def __init__(self):
        self.a=10
        self.b=4
    def callingparent(self):
        super().form1()

g=grandchild()
g.form1()
g.callchildform1()
g.callingparent()

The output is below

bye 6
calling parent from child1
hi 40
bye 6

I can understand the "bye 6" output both the times but how it is printing "hi 40". I am new so anybody can explain what is happening here.

回答1:

You may find the __mro__ attribute of a class informative. Here, MRO stands for Method Resolution Order.

Consider this modification to your code:

class Parent:
    def __init__(self):
        self.a = 2
        self.b = 4

    def print_name(self):
        print("parent")

    def form1(self):
        print("calling parent form1")
        print('p', self.a + self.b)


class Child1(Parent):
    def __init__(self):
        self.a = 50
        self.b = 4

    def print_name(self):
        print("child1")

    def print_super_name(self):
        super().print_name()

    def form1(self):
        print('bye', self.a - self.b)

    def callchildform1(self):
        print("calling parent from child1")
        super().form1()


class Child2(Parent):
    def __init__(self):
        self.a = 3
        self.b = 4

    def print_name(self):
        print("child2")

    def form1(self):
        print('hi', self.a * self.b)

    def callchildform1(self):
        print("calling parent from child2")
        super().form1()


class Grandchild(Child1, Child2):
    def __init__(self):
        self.a = 10
        self.b = 4

    def print_name(self):
        print("grandchild")

    def print_super_name(self):
        super().print_name()

    def print_super_super_name(self):
        super().print_super_name()

    def callingparent(self):
        super().form1()


g = Grandchild()
print("When I print the name of my class it is:")
g.print_name()
print("When I print my superclass name, it is:")
g.print_super_name()
print("When I print the name of the superclass of my superclass, it is:")
g.print_super_super_name()
print("When you call methods on me, they will be executed from my class and my parent classes in the following order:")
print(Grandchild.__mro__)
g.form1()
g.callchildform1()
g.callingparent()

The output is:

When I print the name of my class it is:
grandchild
When I print my superclass name, it is:
child1
When I print the name of the superclass of my superclass, it is:
child2
When you call methods on me, they will be executed from my class and my parent classes in the following order:
(<class '__main__.Grandchild'>, <class '__main__.Child1'>, <class '__main__.Child2'>, <class '__main__.Parent'>, <class 'object'>)
bye 6
calling parent from child1
hi 40
bye 6

When you run g.callchildform1() Python looks for the definition of callchildform1 in Grandchild. It isn't there, so the next place it looks is Child1. You can see from the example and from the method resolution order that when an instance of Grandchild calls a method defined in Child1 which calls super(), the search for the called method will begin in Child2.