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

  1. <tfoot id='epMr2'></tfoot>

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

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

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

      Python:如果不是 val,vs 如果 val 是 None

      Python: if not val, vs if val is None(Python:如果不是 val,vs 如果 val 是 None)

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

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

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

              <tfoot id='cWVCf'></tfoot>
                本文介绍了Python:如果不是 val,vs 如果 val 是 None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我一直以 if not value 的风格进行编码,但是,一些指南引起了我的注意,虽然这种风格有效,但它似乎有两个潜在的问题:

                I've always coded in the style of if not value, however, a few guides have brought to my attention that while this style works, it seems to have 2 potential problems:

                1. 它不完全可读;if value is None 肯定更容易理解.
                2. 这可能会在以后产生影响(并导致细微的错误),因为像 []0 这样的东西也会评估为 False.
                1. It's not completely readable; if value is None is surely more understandable.
                2. This can have implications later (and cause subtle bugs), since things like [] and 0 will evaluate to False as well.

                我也开始将这个想法应用到其他比较中,例如:

                I am also starting to apply this idea to other comparisons, such as:

                • if not value vs if value is False
                • if not value vs if value is []
                • if not value vs if value is False
                • if not value vs if value is []

                列表也是如此......

                And so goes the list...

                问题是,你在原则上走了多远?在保证代码安全的同时,在哪里划清界限?

                The question is, how far do you go with the principle? Where to draw the line, while keeping your code safe?

                无论如何我都应该使用 if value is None 样式吗?

                Should I always use the if value is None style no matter what?

                推荐答案

                如果你想要的话,使用与 None 的比较.使用如果不是价值"如果您只想检查该值是否被认为是假的(空列表,无,假).

                Use a comparison to None if that's what you want. Use "if not value" if you just want to check if the value is considered false (empty list, none, false).

                我发现如果不是价值"看起来更干净和 Pythonic.

                I find "if not value" to be cleaner looking and Pythonic.

                另外,请注意列表.在比较空列表时不应使用 is.如果你知道你得到一个列表,使用 if <list> 检查它是否有任何内容(或 len()).尝试在解释器中输入:

                Also, be careful with lists. You should not use is when comparing for an empty list. If you know you're getting a list, use if <list> to check if it has any contents (or len()). Try typing this into the interpreter:

                >>> a = []
                >>> a is []
                False
                

                这是因为您刚刚创建的临时列表在内存中的地址与存储在a"中的地址不同.你看不到这与 None、False 或 True 因为这些都是单例值(它们都引用内存的同一部分),所以使用 'is' 关键字有效.

                This is because the temporary list you just made has a different address in memory than the one stored at 'a'. You don't see this with None, False, or True because these are all values that are singletons (they all refer to the same section of memory) so using the 'is' keyword works.

                您还会发现 CPython 实习生字符串,因此以下工作.

                You'll also find that CPython interns strings so the following works.

                >>> 'a' is 'a'
                True
                

                你不应该依赖这个.这是一个实现细节,并没有指定适用于每个版本的 Python.

                You should not rely on this. It is an implementation detail and this is not specified to work with every version of Python.

                这篇关于Python:如果不是 val,vs 如果 val 是 None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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='t6XmA'><tr id='t6XmA'><dt id='t6XmA'><q id='t6XmA'><span id='t6XmA'><b id='t6XmA'><form id='t6XmA'><ins id='t6XmA'></ins><ul id='t6XmA'></ul><sub id='t6XmA'></sub></form><legend id='t6XmA'></legend><bdo id='t6XmA'><pre id='t6XmA'><center id='t6XmA'></center></pre></bdo></b><th id='t6XmA'></th></span></q></dt></tr></i><div id='t6XmA'><tfoot id='t6XmA'></tfoot><dl id='t6XmA'><fieldset id='t6XmA'></fieldset></dl></div>

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

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

                          <tbody id='t6XmA'></tbody>

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