<legend id='heRxw'><style id='heRxw'><dir id='heRxw'><q id='heRxw'></q></dir></style></legend>
<i id='heRxw'><tr id='heRxw'><dt id='heRxw'><q id='heRxw'><span id='heRxw'><b id='heRxw'><form id='heRxw'><ins id='heRxw'></ins><ul id='heRxw'></ul><sub id='heRxw'></sub></form><legend id='heRxw'></legend><bdo id='heRxw'><pre id='heRxw'><center id='heRxw'></center></pre></bdo></b><th id='heRxw'></th></span></q></dt></tr></i><div id='heRxw'><tfoot id='heRxw'></tfoot><dl id='heRxw'><fieldset id='heRxw'></fieldset></dl></div>
<tfoot id='heRxw'></tfoot>
    <bdo id='heRxw'></bdo><ul id='heRxw'></ul>

  • <small id='heRxw'></small><noframes id='heRxw'>

      1. Python 的 super() 如何与多重继承一起工作?

        How does Python#39;s super() work with multiple inheritance?(Python 的 super() 如何与多重继承一起工作?)

          <i id='i7qJA'><tr id='i7qJA'><dt id='i7qJA'><q id='i7qJA'><span id='i7qJA'><b id='i7qJA'><form id='i7qJA'><ins id='i7qJA'></ins><ul id='i7qJA'></ul><sub id='i7qJA'></sub></form><legend id='i7qJA'></legend><bdo id='i7qJA'><pre id='i7qJA'><center id='i7qJA'></center></pre></bdo></b><th id='i7qJA'></th></span></q></dt></tr></i><div id='i7qJA'><tfoot id='i7qJA'></tfoot><dl id='i7qJA'><fieldset id='i7qJA'></fieldset></dl></div>

          <tfoot id='i7qJA'></tfoot>
              <bdo id='i7qJA'></bdo><ul id='i7qJA'></ul>
            • <small id='i7qJA'></small><noframes id='i7qJA'>

              1. <legend id='i7qJA'><style id='i7qJA'><dir id='i7qJA'><q id='i7qJA'></q></dir></style></legend>

                  <tbody id='i7qJA'></tbody>
                • 本文介绍了Python 的 super() 如何与多重继承一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I'm pretty much new in Python object oriented programming and I have trouble understanding the super() function (new style classes) especially when it comes to multiple inheritance.

                  For example if you have something like:

                  class First(object):
                      def __init__(self):
                          print "first"
                  
                  class Second(object):
                      def __init__(self):
                          print "second"
                  
                  class Third(First, Second):
                      def __init__(self):
                          super(Third, self).__init__()
                          print "that's it"
                  

                  What I don't get is: will the Third() class inherit both constructor methods? If yes, then which one will be run with super() and why?

                  And what if you want to run the other one? I know it has something to do with Python method resolution order (MRO).

                  解决方案

                  This is detailed with a reasonable amount of detail by Guido himself in his blog post Method Resolution Order (including two earlier attempts).

                  In your example, Third() will call First.__init__. Python looks for each attribute in the class's parents as they are listed left to right. In this case, we are looking for __init__. So, if you define

                  class Third(First, Second):
                      ...
                  

                  Python will start by looking at First, and, if First doesn't have the attribute, then it will look at Second.

                  This situation becomes more complex when inheritance starts crossing paths (for example if First inherited from Second). Read the link above for more details, but, in a nutshell, Python will try to maintain the order in which each class appears on the inheritance list, starting with the child class itself.

                  So, for instance, if you had:

                  class First(object):
                      def __init__(self):
                          print "first"
                  
                  class Second(First):
                      def __init__(self):
                          print "second"
                  
                  class Third(First):
                      def __init__(self):
                          print "third"
                  
                  class Fourth(Second, Third):
                      def __init__(self):
                          super(Fourth, self).__init__()
                          print "that's it"
                  

                  the MRO would be [Fourth, Second, Third, First].

                  By the way: if Python cannot find a coherent method resolution order, it'll raise an exception, instead of falling back to behavior which might surprise the user.

                  Example of an ambiguous MRO:

                  class First(object):
                      def __init__(self):
                          print "first"
                          
                  class Second(First):
                      def __init__(self):
                          print "second"
                  
                  class Third(First, Second):
                      def __init__(self):
                          print "third"
                  

                  Should Third's MRO be [First, Second] or [Second, First]? There's no obvious expectation, and Python will raise an error:

                  TypeError: Error when calling the metaclass bases
                      Cannot create a consistent method resolution order (MRO) for bases Second, First
                  

                  Why do the examples above lack super() calls? The point of the examples is to show how the MRO is constructed. They are not intended to print "first second hird" or whatever. You can – and should, of course, play around with the example, add super() calls, see what happens, and gain a deeper understanding of Python's inheritance model. But my goal here is to keep it simple and show how the MRO is built. And it is built as I explained:

                  >>> Fourth.__mro__
                  (<class '__main__.Fourth'>,
                   <class '__main__.Second'>, <class '__main__.Third'>,
                   <class '__main__.First'>,
                   <type 'object'>)
                  

                  这篇关于Python 的 super() 如何与多重继承一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

                  1. <tfoot id='9Qvvt'></tfoot>
                  2. <i id='9Qvvt'><tr id='9Qvvt'><dt id='9Qvvt'><q id='9Qvvt'><span id='9Qvvt'><b id='9Qvvt'><form id='9Qvvt'><ins id='9Qvvt'></ins><ul id='9Qvvt'></ul><sub id='9Qvvt'></sub></form><legend id='9Qvvt'></legend><bdo id='9Qvvt'><pre id='9Qvvt'><center id='9Qvvt'></center></pre></bdo></b><th id='9Qvvt'></th></span></q></dt></tr></i><div id='9Qvvt'><tfoot id='9Qvvt'></tfoot><dl id='9Qvvt'><fieldset id='9Qvvt'></fieldset></dl></div>
                  3. <legend id='9Qvvt'><style id='9Qvvt'><dir id='9Qvvt'><q id='9Qvvt'></q></dir></style></legend>
                      <bdo id='9Qvvt'></bdo><ul id='9Qvvt'></ul>

                      <small id='9Qvvt'></small><noframes id='9Qvvt'>

                            <tbody id='9Qvvt'></tbody>