尝试“钻石形状"问题时,带有OOP的Python在这里发生了什么 [英] What is happening here in Python with OOPs while trying Diamond Shape problem
问题描述
我正在用python学习OOP.在下面的代码中创建,以在多个继承中复制菱形问题.我正在jupyter笔记本中运行以下代码,并且输出是同时生成的.
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()
输出如下
bye 6
calling parent from child1
hi 40
bye 6
我两次都可以理解"bye 6"输出,但是它是如何打印"hi 40"的.我是新来的,所以任何人都可以解释这里发生的事情.
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.
推荐答案
您可能会找到信息丰富的类的__mro__
属性.在这里,MRO代表 M ethod R 解决方案 O rder.
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()
输出为:
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
运行g.callchildform1()
时,Python在Grandchild
中查找callchildform1
的定义.它不在那里,所以下一个看起来是Child1
.您可以从示例和方法解析顺序中看到,当Grandchild
的实例调用Child1
中定义的方法(该方法调用super()
)时,对被调用方法的搜索将从Child2
开始.
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
.
这篇关于尝试“钻石形状"问题时,带有OOP的Python在这里发生了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!