如果x :, vs如果x == True,vs如果x为True [英] if x:, vs if x == True, vs if x is True

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

问题描述

很抱歉,是否曾经有人问过这个问题,但是我徒劳地搜索了我的 exact 问题的答案.基本上,在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不必在字面上是"True"即可运行该函数.例如:

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 == True x为True 则不是这种情况.看到PEP 8强烈建议仅使用 if x:变体,有人可以解释为什么会发生这种现象吗?看来,如果x:更多地是对如果x不为假"或如果x存在"的检验.考虑到这一点,我相信如果x为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:

  • False
  • None
  • 等于0的数字值,例如00.0-0.0
  • 空字符串:''u''
  • 空容器(例如列表,元组和字典)
  • 任何实现__bool__(在Python3中)返回False__nonzero__(在Python2中)返回False0的东西.
  • 任何不实现__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

如果某个对象适用,则将其视为"false",否则将视为"true",无论该对象实际上与False还是True

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之一,则可以安全地编写if 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,则您处于两种情况之一,因此不会写if x is True.重要的是正确记录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是true则运行该函数",那么这表示您的原始代码将执行什么操作.对其进行记录,如果x is True,则运行功能"将具有不同的含义,并且由于PEP8中的样式规则要求测试真实性而不是特定值True,因此不太常用.

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情况下的行为有所不同,则可以编写类似if x is not 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.

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

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