if x:, vs if x == True, vs if x is True [英] if x:, vs if x == True, vs if x is True

查看:32
本文介绍了if x:, vs if x == True, vs if x is True的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果之前有人问过这个问题,我深表歉意,但我一直在寻找我的确切问题的答案,但徒劳无功.基本上,使用 Python 2.7,我有一个运行一系列地理处理工具的程序,这取决于用户在脚本中调整的一系列 True/False 变量所要求的内容,例如

Apologies if this has been asked before, but I have searched in vain for an answer to my exact question. Basically, with Python 2.7, I have a program running a series of geoprocessing tools, depended on what is reqested via a series of True/False variables that the user adjusts in the script e.g.

x = True

if x:
    run function

然而,我现在发现 x 不需要是字面上的真";使函数运行.例如:

However, I have now discovered that x does not need to be literally "True" for the function to run. For example:

In: x = True
    if x:
        print True

Out: True

In: x = 123
    if x:
        print True

Out: True

In: x = 'False'
    if x:
        print True

Out: True

In: x = False
    if x:
        print True

Out: 

因此,除 False 以外的任何值似乎都计算为 True,如果 x == Truex 为 True,则情况并非如此.看到 PEP 8 强烈建议只使用 if x: 变体,有人能解释为什么会发生这种行为吗?似乎 if x: 更像是对if x is not False"的测试.或如果 x 存在".考虑到这一点,我认为我应该使用 if x is True:在这种情况下,不管 PEP 8 怎么说.

So any value other than False appears to evaluate to True, which would not be the case for if x == True or if x is True. Seeing as PEP 8 strongly recommends only using the if x: variant, can anybody explain why this behaviour occurs? It seems that if x: is more a test for "if x is not False" or "if x exists". With that in mind, I believe I should be using if x is True: in this case, despite what PEP 8 has to say.

推荐答案

Python 中的以下值在 if 和其他逻辑上下文的上下文中为 false:

The following values in Python are false in the context of if and other logical contexts:

  • 等于0的数值,如00.0-0.0
  • 空字符串:''u''
  • 空容器(例如列表、元组和字典)
  • 任何实现 __bool__(在 Python3 中)返回 False,或 __nonzero__(在 Python2 中)返回 Falsecode> 或 0.
  • 任何不实现 __bool__(在 Python3 中)或 __nonzero__(在 Python2 中)但确实实现了 __len__ 以返回值的东西等于0
  • False
  • None
  • numeric values equal to 0, such as 0, 0.0, -0.0
  • empty strings: '' and u''
  • empty containers (such as lists, tuples and dictionaries)
  • anything that implements __bool__ (in Python3) to return False, or __nonzero__ (in Python2) to return False or 0.
  • anything that doesn't implement __bool__ (in Python3) or __nonzero__ (in Python2), but does implement __len__ to return a value equal to 0

如果其中任何一个适用,则对象被认为是假",否则被认为是真",无论它实际上是否等于或等同于 FalseTrue

An object is considered "false" if any of those applies, and "true" otherwise, regardless of whether it's actually equal to or identical with False or True

现在,如果您已经安排 x 必须是对象 TrueFalse 之一,那么您可以安全地编写 如果x.如果你已经安排了 x 的真实性"指示是否执行操作,不管类型如何,那么你可以安全地编写 if x.您可以在哪里写您应该更喜欢这样做,因为它更易于阅读.

Now, if you've arranged that x is necessarily one of the objects True or False, then you can safely write if x. If you've arranged that the "trueness" of x indicates whether or not to perform the operation, regardless of type, then you can safely write if x. Where you can write that you should prefer to do so, since it's cleaner to read.

通常,如果允许 x 取值 True,那么您处于这两种情况之一,因此您不会编写 如果 x 为真.重要的是要正确记录x的含义,使其反映代码中使用的测试.

Normally, if it is allowed for x to take the value True then you're in one of those two cases, and so you would not write if x is True. The important thing is to correctly document the meaning of x, so that it reflects the test used in the code.

Python 程序员应该知道什么是正确的,所以如果你只是记录,如果 x 为真,则运行函数",那么这表达了原始代码的作用.记录它,如果 x 为真,则运行该函数"会有不同的含义,并且不太常用,正是因为 PEP8 中的样式规则说测试真实性而不是特定值 <代码>真.

Python programmers are expected to know what's considered true, so if you just document, "runs the function if x is true", then that expresses what your original code does. Documenting it, "runs the function if x is True" would have a different meaning, and is less commonly used precisely because of the style rule in PEP8 that says to test for trueness rather than the specific value True.

但是,如果您希望代码在 x 是空容器的情况下与 None 的情况不同,那么您可以编写类似如果 x 不是 None.

However, if you wanted the code to behave differently in the case where x is an empty container from the case where it is None, then you would write something like if x is not None.

这篇关于if x:, vs if x == True, vs if x is True的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆