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

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

        <tfoot id='ErOxd'></tfoot>

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

      2. Python 中的“x = y 或 z"赋值有什么作用?

        What does an #39;x = y or z#39; assignment do in Python?(Python 中的“x = y 或 z赋值有什么作用?)
      3. <i id='RtQm5'><tr id='RtQm5'><dt id='RtQm5'><q id='RtQm5'><span id='RtQm5'><b id='RtQm5'><form id='RtQm5'><ins id='RtQm5'></ins><ul id='RtQm5'></ul><sub id='RtQm5'></sub></form><legend id='RtQm5'></legend><bdo id='RtQm5'><pre id='RtQm5'><center id='RtQm5'></center></pre></bdo></b><th id='RtQm5'></th></span></q></dt></tr></i><div id='RtQm5'><tfoot id='RtQm5'></tfoot><dl id='RtQm5'><fieldset id='RtQm5'></fieldset></dl></div>
          <bdo id='RtQm5'></bdo><ul id='RtQm5'></ul>
            <tbody id='RtQm5'></tbody>

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

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

                <tfoot id='RtQm5'></tfoot>

                • 本文介绍了Python 中的“x = y 或 z"赋值有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  例如:

                  def my_function(arg_1=None, arg_2=0):
                      determination = arg_1 or arg_2 or 'no arguments given!'
                      print(determination)
                      return determination
                  

                  当不带参数调用时,上述函数将打印并返回 'no arguments given!'

                  When called with no arguments, the above function would print and return 'no arguments given!'

                  Python 为什么要这样做,如何才能最好地利用这一功能?

                  Why does Python do this, and how can one best make best use of this functionality?

                  推荐答案

                  or"表达式在赋值时的作用:

                  我们有时会在 Python 中看到这样的示例,以替代具有三元赋值的条件表达式,(实际上,它有助于激发语言添加条件语句).

                  What the "or" expression does on assignment:

                  We sometimes see examples of this in Python as a substitute for conditional expression with ternary assignments, (in fact, it helped inspire the language to add conditional statements).

                  x = a or b
                  

                  如果bool(a)返回False,则x被赋值为b

                  这里有一个这样的条件表达式的例子,它完成了同样的事情,但可能没有那么神秘.

                  Here's an example of such a conditional expression that accomplishes the same thing, but with perhaps a bit less mystery.

                  def my_function(arg_1=None, arg_2=0):
                      determination = arg_1 if arg_1 else arg_2 if arg_2 else 'no arguments given!'
                      print(determination)
                      return determination
                  

                  重复这种语法太多被认为是不好的风格,否则 它的对于单行来说还可以. 缺点是它 有点重复.

                  Repeating this syntax too much is considered to be bad style, otherwise it's OK for one-liners. The downside is that it is a bit repetitive.

                  基本情况,x 或 y 如果 bool(x) 评估为 True,则返回 x,否则返回评估 y,(查看文档以供参考).因此,一系列 or 表达式具有返回评估 True 的第一项或最后一项的效果.

                  The base case, x or y returns x if bool(x) evaluates True, else it evaluates y, (see the docs for reference). Therefore, a series of or expressions has the effect of returning the first item that evaluates True, or the last item.

                  例如

                  '' or [] or 'apple' or () or set(['banana'])
                  

                  返回 'apple',第一个计算结果为 True 的项目,以及

                  returns 'apple', the first item that evaluates as True, and

                  '' or [] or ()
                  

                  返回 (),即使它的计算结果为 False.

                  returns (), even though it evaluates as False.

                  相比之下,如果 bool(x) 评估为 False,则 x 和 y 返回 x,否则返回返回 y.

                  For contrast, x and y returns x if bool(x) evaluates as False, else it returns y.

                  当您考虑到条件 and 系列中的所有条件都需要评估为 Trueand 会以这种方式工作是有道理的> 让控制流沿着这条路径前进,并且当您遇到 False 的项目时继续评估这些项目是没有意义的.

                  It makes sense that and would work this way when you consider that all of the conditions in a conditional and series needs to evaluate as True for the control flow to proceed down that path, and that it makes no sense to continue evaluating those items when you come across one that is False.

                  使用 and 进行赋值的实用性并不像使用 or 那样立即明显,但它在历史上曾用于三元赋值.也就是说,在这种更清晰和直接的构造可用之前:

                  The utility of using and for assignment is not immediately as apparent as using or, but it was historically used for ternary assignment. That is, before this more clear and straightforward construction was available:

                  a = x if condition else y
                  

                  用布尔运算符形成的等价物是:

                  the equivalent formed with boolean operators was:

                  a = condition and x or z                      # don't do this!
                  

                  虽然它的含义是基于对 Python andor 评估的充分理解而得出的,但它的可读性不如三元条件,最好完全避免.

                  which while the meaning is derivable based on a full understanding of Python and and or evaluation, is not nearly as readable as the ternary conditional, and is best avoided altogether.

                  必须小心使用布尔表达式进行赋值.绝对不要使用 and 进行赋值,这很容易混淆,很容易出错.样式专家会发现使用 or 的赋值不太可取(比更冗长的三元,if 条件 else),但我发现它在专业的 Python 社区中如此普遍,以至于它可以被认为是惯用的.

                  Using Boolean expressions for assignment must be done carefully. Definitely never use and for assignment, which is confusing enough to be quite error-prone. Style mavens will find use of or for assignments less preferable (than the more verbose ternary, if condition else), but I have found that it is so common in the professional Python community that it could be considered idiomatic.

                  如果您选择使用它,请谨慎使用,并理解如果达到最终元素,无论其评估如何,都将始终返回,因此最终元素可能应该是 literal,这样你就知道你的变量有一个很好的默认后备.

                  If you choose to use it, do so cautiously with the understanding that the final element, if reached, will always be returned regardless of its evaluation, so that final element should probably be a literal, so that you know you have a good default fallback for your variable.

                  这篇关于Python 中的“x = y 或 z"赋值有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 与否?)

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

                      <tbody id='OyvTE'></tbody>

                      <legend id='OyvTE'><style id='OyvTE'><dir id='OyvTE'><q id='OyvTE'></q></dir></style></legend>
                        <tfoot id='OyvTE'></tfoot>

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