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

      2. <small id='VvYN6'></small><noframes id='VvYN6'>

        为什么在 Python 中执行“0, 0 == (0, 0)"?等于“(0,假)"?

        Why in Python does quot;0, 0 == (0, 0)quot; equal quot;(0, False)quot;?(为什么在 Python 中执行“0, 0 == (0, 0)?等于“(0,假)?)

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

          1. <legend id='DrtAC'><style id='DrtAC'><dir id='DrtAC'><q id='DrtAC'></q></dir></style></legend>
              <tbody id='DrtAC'></tbody>
            <tfoot id='DrtAC'></tfoot>
              <bdo id='DrtAC'></bdo><ul id='DrtAC'></ul>

                • <i id='DrtAC'><tr id='DrtAC'><dt id='DrtAC'><q id='DrtAC'><span id='DrtAC'><b id='DrtAC'><form id='DrtAC'><ins id='DrtAC'></ins><ul id='DrtAC'></ul><sub id='DrtAC'></sub></form><legend id='DrtAC'></legend><bdo id='DrtAC'><pre id='DrtAC'><center id='DrtAC'></center></pre></bdo></b><th id='DrtAC'></th></span></q></dt></tr></i><div id='DrtAC'><tfoot id='DrtAC'></tfoot><dl id='DrtAC'><fieldset id='DrtAC'></fieldset></dl></div>
                  本文介绍了为什么在 Python 中执行“0, 0 == (0, 0)"?等于“(0,假)"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Python 中(我只检查了 Python 3.6,但我相信它也应该适用于许多以前的版本):

                  In Python (I checked only with Python 3.6 but I believe it should hold for many of the previous versions as well):

                  (0, 0) == 0, 0   # results in a two element tuple: (False, 0)
                  0, 0 == (0, 0)   # results in a two element tuple: (0, False)
                  (0, 0) == (0, 0) # results in a boolean True
                  

                  但是:

                  a = 0, 0
                  b = (0, 0)
                  a == b # results in a boolean True
                  

                  为什么两种方法的结果不同?相等运算符是否以不同方式处理元组?

                  Why does the result differ between the two approaches? Does the equality operator handle tuples differently?

                  推荐答案

                  前两个表达式都解析为元组:

                  The first two expressions both parse as tuples:

                  1. (0, 0) == 0(即False),后跟0
                  2. 0,然后是 0 == (0, 0)(这样仍然是 False).
                  1. (0, 0) == 0 (which is False), followed by 0
                  2. 0, followed by 0 == (0, 0) (which is still False that way around).

                  由于逗号分隔符相对于相等运算符的相对优先级,表达式被拆分为:Python 看到一个包含两个表达式的元组,其中一个恰好是相等测试,而不是两个元组之间的相等测试.

                  The expressions are split that way because of the relative precedence of the comma separator compared to the equality operator: Python sees a tuple containing two expressions, one of which happens to be an equality test, instead of an equality test between two tuples.

                  但是在您的第二组语句中,a = 0, 0 不能 是一个元组.元组是值的集合,与相等测试不同,赋值在 Python 中没有值.赋值不是表达式,而是语句;它没有可以包含在元组或任何其他周围表达式中的值.如果您尝试使用 (a = 0), 0 之类的方法来强制解释为元组,则会出现语法错误.这就留下了将元组分配给一个变量——这可以通过写成 a = (0, 0) 来更明确——作为 a = 0, 0<的唯一有效解释/代码>.

                  But in your second set of statements, a = 0, 0 cannot be a tuple. A tuple is a collection of values, and unlike an equality test, assignment has no value in Python. An assignment is not an expression, but a statement; it does not have a value that can be included into a tuple or any other surrounding expression. If you tried something like (a = 0), 0 in order to force interpretation as a tuple, you would get a syntax error. That leaves the assignment of a tuple to a variable – which could be made more explicit by writing it a = (0, 0) – as the only valid interpretation of a = 0, 0.

                  所以即使在赋值给 a 时没有括号,它和 b 都被赋值为 (0,0),所以a == b 因此是 True.

                  So even without the parentheses on the assignment to a, both it and b get assigned the value (0,0), so a == b is therefore True.

                  这篇关于为什么在 Python 中执行“0, 0 == (0, 0)"?等于“(0,假)"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                  <i id='LO0TQ'><tr id='LO0TQ'><dt id='LO0TQ'><q id='LO0TQ'><span id='LO0TQ'><b id='LO0TQ'><form id='LO0TQ'><ins id='LO0TQ'></ins><ul id='LO0TQ'></ul><sub id='LO0TQ'></sub></form><legend id='LO0TQ'></legend><bdo id='LO0TQ'><pre id='LO0TQ'><center id='LO0TQ'></center></pre></bdo></b><th id='LO0TQ'></th></span></q></dt></tr></i><div id='LO0TQ'><tfoot id='LO0TQ'></tfoot><dl id='LO0TQ'><fieldset id='LO0TQ'></fieldset></dl></div>

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

                  <tfoot id='LO0TQ'></tfoot>
                  <legend id='LO0TQ'><style id='LO0TQ'><dir id='LO0TQ'><q id='LO0TQ'></q></dir></style></legend>
                    <bdo id='LO0TQ'></bdo><ul id='LO0TQ'></ul>
                        <tbody id='LO0TQ'></tbody>