本文介绍了什么是Python中的兄弟类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 Python 2文档说 super()函数 返回一个代理对象,它将方法调用委托给父类或兄弟类的类型。 问题: Python中的兄弟类是什么? 如何将方法调用委托给兄弟类? 我的假设是给定类的兄弟是一个继承自同一父类的类。我起草了以下代码,以了解如何将方法调用委托给兄弟,但它不起作用。我该怎么做或理解错了? class ClassA(object): def MethodA(self): printClassA的MethodA class ClassB(ClassA): def MethodB(self): printClassB的MethodB class ClassC(ClassA): def MethodA(self): super(ClassC,self).MethodA() def MethodB(self) : super(ClassC,self).MethodB() if __name__ =='__ main__': ClassC()。MethodA()#按预期工作 #尝试将方法委托给兄弟时失败。 ClassC()。MethodB()#AttirbuteError:'super'对象没有属性'MethodB' 解决方案经过一番进一步的研究和阅读 Python的超级()被认为超级!文章我得出以下结论: 兄弟班就是我的想法。它是一个继承自同一父级的类。正是Python文档的定义让我不再参与其中。似乎当Python文档说委托对父或兄弟类的方法调用时,它意味着到父或父的兄弟,这也是给定孩子的基类。这是必须发生的钻石继承。 super() function将方法调用委托给a 父母的兄弟类自动基于MRO(方法解析顺序)。 这是一个有趣的我在试验 super()函数时发现的情况: class Class0(object): def MethodA(self): print(MethodA of Class0) class ClassA(Class0): def MethodA(self): super(ClassA,self).MethodA() print(ClassA的MethodA) class ClassB(Class0): def MethodA(self): print(ClassB的MethodA) class ClassC(ClassA,ClassB): def MethodA(self): super (ClassC,self).MethodA() if __name__ =='__ main__': ClassC()。MethodA() 代码将打印 ClassB的MethodA ClassA的MethodA 如果你像我一样想知道为什么 Class0 的MethodA永远不会被打印出来,这就是我理解的解释。用打印(ClassC .__ mro __)打印的ClassC的MRO是 ( < class'__main __。ClassC'>,< class'__main __。ClassA'>,< class'__ main __。ClassB'>,< class'__main __。Class0'>,< class'object' >)。 现在,如果您按照MRO,ClassC的MethodA()的super()函数将调用ClassA的MethodA(),在打印之前将调用MethodA() classB(因为它是MRO的下一个)。而且,ClassB的MethodA()只会打印并退出,因为它不使用super()函数将方法调用委托给MRO链进一步调整。 Python 2 documentation says that super() function "returns a proxy object that delegates method calls to a parent or sibling class of type."The questions:What is a sibling class in Python?How do you delegate a method call to a sibling class?My presumption was that a sibling for a given class is a class that inherits from the same parent. I drafted the following code to see how a method call can be delegated to a sibling, but it didn't work. What do I do or understand wrong?class ClassA(object): def MethodA(self): print "MethodA of ClassA"class ClassB(ClassA): def MethodB(self): print "MethodB of ClassB"class ClassC(ClassA): def MethodA(self): super(ClassC, self).MethodA() def MethodB(self): super(ClassC, self).MethodB()if __name__ == '__main__': ClassC().MethodA() # Works as expected # Fail while trying to delegate method to a sibling. ClassC().MethodB() # AttirbuteError: 'super' object has no attribute 'MethodB' 解决方案 After some further research and reading Python’s super() considered super! article I came to the following conclusions:A sibling class is what I was thinking it was. It is a class that inherits from the same parent. It is the definition of the Python documentation that threw me off the course. It seems that when Python documentation says delegates method calls to a parent or sibling class it means to a parent or parent's sibling which is also a base class of a given child. That is a diamond inheritance must take place.super() function delegates a method call to a parent's sibling class automatically based on MRO (method resolution order).Here's an interesting case I found while experimenting with super() function:class Class0(object): def MethodA(self): print("MethodA of Class0")class ClassA(Class0): def MethodA(self): super(ClassA, self).MethodA() print("MethodA of ClassA")class ClassB(Class0): def MethodA(self): print("MethodA of ClassB")class ClassC(ClassA, ClassB): def MethodA(self): super(ClassC, self).MethodA()if __name__ == '__main__': ClassC().MethodA()The code will printMethodA of ClassBMethodA of ClassAIf you as me wonder why MethodA of Class0 never gets printed, here is the explanation as I understand it. The MRO of the ClassC as printed with print(ClassC.__mro__) is (<class '__main__.ClassC'>, <class '__main__.ClassA'>, <class '__main__.ClassB'>, <class '__main__.Class0'>, <class 'object'>). Now if you follow the MRO the super() function of MethodA() of ClassC will call the MethodA() of ClassA, which before printing will call MethodA() of classB (since it's next in MRO). And the MethodA() of ClassB in its turn will just print and exit since it doesn't use super() function to delegate the method call further up the MRO chain. 这篇关于什么是Python中的兄弟类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-23 21:19