Python:if not val,vs if val is None
我一直以 if not value
的风格进行编码,但是,一些指南引起了我的注意,虽然这种风格有效,但它似乎有两个潜在的问题:
- 它不完全可读;
if value is None
肯定更容易理解。 - 这可能会在以后产生影响(并导致微妙的错误),因为
[]
和0
之类的东西也会评估为False
。
我也开始将这个想法应用到其他比较中,例如:
if not value
vsif value is False
if not value
vsif value is []
列表也是如此...
问题是,你对这个原则的了解有多远?在保证代码安全的同时,在哪里划清界限?
无论如何,我应该始终使用 if value is None
样式吗?
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:
- It's not completely readable;
if value is None
is surely more understandable. - This can have implications later (and cause subtle bugs), since things like
[]
and0
will evaluate toFalse
as well.
I am also starting to apply this idea to other comparisons, such as:
if not value
vsif value is False
if not value
vsif 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?
Should I always use the if value is None
style no matter what?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您想要的话,请使用与 None 的比较。如果您只想检查该值是否被视为 false(空列表、none、false),请使用“if not value”。
我发现“如果没有价值”看起来更干净并且Pythonic。
另外,要小心列表。比较空列表时不应使用 is。如果您知道自己正在获取一个列表,请使用
if
检查它是否有任何内容(或 len())。尝试在解释器中输入以下内容:这是因为您刚刚创建的临时列表在内存中的地址与存储在“a”中的地址不同。您不会在 None、False 或 True 中看到这一点,因为这些都是单例值(它们都引用内存的同一部分),因此使用“is”关键字是有效的。
您还会发现 CPython 会实习生字符串,因此以下内容有效。
您不应该依赖于此。这是一个实现细节,并未指定适用于每个版本的 Python。
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).
I find "if not value" to be cleaner looking and Pythonic.
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: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.
You'll also find that CPython interns strings so the following works.
You should not rely on this. It is an implementation detail and this is not specified to work with every version of Python.
不可以。如果您想在值为 false 但不是
None
时运行代码,这会严重失败。如果您要使用
None
对象检查身份,请使用is None
。如果您只想让值为 False,请使用not value
。No. If you want to run code when the value is false but isn't
None
, this would fail horribly.Use
is None
if you're checking for identity with theNone
object. Usenot value
if you just want the value to be False.我的答案很简单,因为它适用于大多数编码问题:不要尝试编写可以正常工作的东西。尝试尽可能清楚地表达您的意图。如果您想检查某个值是否为 false,请使用
if not value
。如果您想检查None
,请将其写下来。这始终取决于情况和您的判断。你不应该试图寻找无需思考即可应用的规则。如果你找到了这些规则,那么这就是计算机的工作,而不是人类的工作! ;-)
My answer is simple, as it applies to most coding problems: Don't try to write something that just works. Try to express your intent as clearly as possible. If you want to check if a value is false, use
if not value
. If you want to check forNone
, write it down. It always depends on the situation and your judgement.You should not try to find rules which can be applied without thinking. If you find those rules, it's a job for a computer, not for a human! ;-)
您对
is
运算符的使用有点问题。例如,if value is []
将始终为 false,因为没有两个活动列表具有相同的标识。它与None
配合使用效果很好,因为None
是单例(对None
的所有引用都是同一对象),但对于其他比较,请使用==。
然而,
if value
和if not value
是完全可读且有用的。恕我直言,没有必要更具体,除非您需要以不同的方式处理各种类型的真值或假值,例如区分 0 和None
。Your use of the
is
operator is a little problematic.if value is []
will always be false, for example, because no two active lists have the same identity. It works great withNone
becauseNone
is a singleton (all references toNone
are the same object) but for other comparisons, use==
.However,
if value
andif not value
are perfectly readable and useful. IMHO there's no need to be more specific, unless you need to treat various types of truthy or falsy values differently, as, for example, distinguishing between 0 andNone
.很好,“Pythonic”并且在大多数情况下是首选。它不会导致微妙的错误,规则是明确的,并且(我发现)
如果您需要区分 False 和 None,如您所提到的,请使用:
不过,我发现上述内容很少有必要。 ElementTree 元素是 XML 复杂性得以凸显的示例之一,因为存在不止一种真实性测试。节点,以及它是否包含其他节点。
一般来说,优先考虑的是创造积极的条件,并将其放在首位。它们更容易一目了然,并且随着复杂性的增加而保持良好的状态(似乎总是如此)。
is fine, "pythonic" and preferred in most cases. It does not cause subtle bugs, the rules are explicit and (I find) easy to understand.
If you need to differentiate between False and None as you mentioned, use:
I find the above rarely necessary though. ElementTree elements are one example where XML's complexity shines through, due to there being more than one test of truth. The node, and whether it contains others.
In general, it is preferred to make positive conditions, and put them first. They are easier to understand at a glance and hold up well as complexity increases (as it always seems to).