<legend id='mPeNK'><style id='mPeNK'><dir id='mPeNK'><q id='mPeNK'></q></dir></style></legend>

<small id='mPeNK'></small><noframes id='mPeNK'>

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

        多态性在 Python 中是如何工作的?

        How does polymorphism work in Python?(多态性在 Python 中是如何工作的?)
        • <small id='s8cTD'></small><noframes id='s8cTD'>

          <legend id='s8cTD'><style id='s8cTD'><dir id='s8cTD'><q id='s8cTD'></q></dir></style></legend>

        • <tfoot id='s8cTD'></tfoot>
            <tbody id='s8cTD'></tbody>

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

                  本文介绍了多态性在 Python 中是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 Python 的新手...并且主要来自 Java 背景,如果这说明了什么的话.

                  I'm new to Python... and coming from a mostly Java background, if that accounts for anything.

                  我正在尝试理解 Python 中的多态性.也许问题在于我希望将我已经知道的概念投射到 Python 中.但是我把下面的测试代码放在一起:

                  I'm trying to understand polymorphism in Python. Maybe the problem is that I'm expecting the concepts I already know to project into Python. But I put together the following test code:

                  class animal(object):
                      "empty animal class"
                  
                  class dog(animal):
                      "empty dog class"
                  
                  myDog = dog()
                  print myDog.__class__ is animal
                  print myDog.__class__ is dog
                  

                  根据我习惯的多态性(例如 java 的 instanceof),我希望这两个语句都打印为 true,因为 dog is an animal 的实例而且是一只狗.但我的输出是:

                  From the polymorphism I'm used to (e.g. java's instanceof), I would expect both of these statements to print true, as an instance of dog is an animal and also is a dog. But my output is:

                  False
                  True
                  

                  我错过了什么?

                  推荐答案

                  Python 中的 is 运算符检查两个参数是否引用了内存中的同一个对象;它不像 C# 中的 is 运算符.

                  The is operator in Python checks that the two arguments refer to the same object in memory; it is not like the is operator in C#.

                  来自文档:

                  运算符 is 和 is not 测试对象身份:当且仅当 x 和 y 是同一个对象时,x 是 y 为真.x is not y 产生逆真值.

                  The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.

                  在这种情况下,您要查找的是 isinstance.

                  What you're looking for in this case is isinstance.

                  如果 object 参数是 classinfo 参数或其(直接或间接)子类的实例,则返回 true.

                  Return true if the object argument is an instance of the classinfo argument, or of a (direct or indirect) subclass thereof.

                  >>> class animal(object): pass
                  
                  >>> class dog(animal): pass
                  
                  >>> myDog = dog()
                  >>> isinstance(myDog, dog)
                  True
                  >>> isinstance(myDog, animal)
                  True
                  

                  然而,惯用的 Python 要求您(几乎)从不进行类型检查,而是依赖 duck-为多态行为键入.使用 isinstance 来理解继承并没有错,但在生产"代码中通常应该避免使用它.

                  However, idiomatic Python dictates that you (almost) never do type-checking, but instead rely on duck-typing for polymorphic behavior. There's nothing wrong with using isinstance to understand inheritance, but it should generally be avoided in "production" code.

                  这篇关于多态性在 Python 中是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
                1. <i id='kjRZy'><tr id='kjRZy'><dt id='kjRZy'><q id='kjRZy'><span id='kjRZy'><b id='kjRZy'><form id='kjRZy'><ins id='kjRZy'></ins><ul id='kjRZy'></ul><sub id='kjRZy'></sub></form><legend id='kjRZy'></legend><bdo id='kjRZy'><pre id='kjRZy'><center id='kjRZy'></center></pre></bdo></b><th id='kjRZy'></th></span></q></dt></tr></i><div id='kjRZy'><tfoot id='kjRZy'></tfoot><dl id='kjRZy'><fieldset id='kjRZy'></fieldset></dl></div>

                  <tfoot id='kjRZy'></tfoot>

                  1. <small id='kjRZy'></small><noframes id='kjRZy'>

                      <tbody id='kjRZy'></tbody>

                      • <legend id='kjRZy'><style id='kjRZy'><dir id='kjRZy'><q id='kjRZy'></q></dir></style></legend>

                            <bdo id='kjRZy'></bdo><ul id='kjRZy'></ul>