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

    <legend id='myE4q'><style id='myE4q'><dir id='myE4q'><q id='myE4q'></q></dir></style></legend>
    <tfoot id='myE4q'></tfoot>
    1. <small id='myE4q'></small><noframes id='myE4q'>

      • <bdo id='myE4q'></bdo><ul id='myE4q'></ul>

        Python - “if"中的逻辑评估顺序陈述

        时间:2023-09-01
      1. <i id='jRnwB'><tr id='jRnwB'><dt id='jRnwB'><q id='jRnwB'><span id='jRnwB'><b id='jRnwB'><form id='jRnwB'><ins id='jRnwB'></ins><ul id='jRnwB'></ul><sub id='jRnwB'></sub></form><legend id='jRnwB'></legend><bdo id='jRnwB'><pre id='jRnwB'><center id='jRnwB'></center></pre></bdo></b><th id='jRnwB'></th></span></q></dt></tr></i><div id='jRnwB'><tfoot id='jRnwB'></tfoot><dl id='jRnwB'><fieldset id='jRnwB'></fieldset></dl></div>

        <tfoot id='jRnwB'></tfoot>

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

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

              <tbody id='jRnwB'></tbody>
                • <bdo id='jRnwB'></bdo><ul id='jRnwB'></ul>

                  本文介绍了Python - “if"中的逻辑评估顺序陈述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Python 中我们可以这样做:

                  In Python we can do this:

                  if True or blah:
                      print("it's ok") # will be executed
                  
                  if blah or True: # will raise a NameError
                      print("it's not ok")
                  
                  class Blah:
                      pass
                  blah = Blah()
                  
                  if blah or blah.notexist:
                      print("it's ok") # also will be executed
                  

                  • 有人可以指点我有关此功能的文档吗?
                  • 是语言的实现细节还是特性?
                  • 利用此功能是一种好的编码风格吗?
                  • 推荐答案

                    orand 短路,见布尔运算 文档:

                    表达式x and y首先计算x;如果 x 为 false,则返回其值;否则,评估 y 并返回结果值.

                    The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

                    表达式x or y首先计算x;如果 x 为真,则返回其值;否则,评估 y 并返回结果值.

                    The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

                    请注意,对于 ,如果 x 的计算结果为 True 值,y 仅 被计算.相反,对于 ory 仅在 x 评估为 False 值时才评估.

                    Note how, for and, y is only evaluated if x evaluates to a True value. Inversely, for or, y is only evaluated if x evaluated to a False value.

                    对于第一个表达式True or blah,这意味着永远不会评估blah,因为第一部分已经是True.

                    For the first expression True or blah, this means that blah is never evaluated, since the first part is already True.

                    此外,您的自定义 Blah 类被认为是 True:

                    Furthermore, your custom Blah class is considered True:

                    在布尔运算的上下文中,以及当控制流语句使用表达式时,以下值被解释为假:FalseNone、数字零所有类型,以及空字符串和容器(包括字符串、元组、列表、字典、集合和frozensets).所有其他值都被解释为 true.(参见 __nonzero__() 特殊方法改变这一点.)

                    In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true. (See the __nonzero__() special method for a way to change this.)

                    由于您的类没有实现 __nonzero__() 方法(也没有实现 __len__ 方法),因此就布尔值而言,它被视为 True表达有关.

                    Since your class does not implement a __nonzero__() method (nor a __len__ method), it is considered True as far as boolean expressions are concerned.

                    在表达式 blah 或 blah.notexist 中,blah 因此为真,并且永远不会评估 blah.notexist.

                    In the expression blah or blah.notexist, blah is thus true, and blah.notexist is never evaluated.

                    经验丰富的开发人员经常有效地使用此功能,最常用于指定默认值:

                    This feature is used quite regularly and effectively by experienced developers, most often to specify defaults:

                    some_setting = user_supplied_value or 'default literal'
                    object_test = is_it_defined and is_it_defined.some_attribute
                    

                    请小心链接这些内容,并在适用的情况下使用条件表达式.

                    Do be wary of chaining these and use a conditional expression instead where applicable.

                    这篇关于Python - “if"中的逻辑评估顺序陈述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                      <bdo id='34pWG'></bdo><ul id='34pWG'></ul>
                      <tfoot id='34pWG'></tfoot>
                        <tbody id='34pWG'></tbody>

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

                    • <small id='34pWG'></small><noframes id='34pWG'>

                      <legend id='34pWG'><style id='34pWG'><dir id='34pWG'><q id='34pWG'></q></dir></style></legend>