Python的布尔 - 如果X:VS如果x ==真,VS如果x为True [英] Python booleans - if x:, vs if x == True, vs if x is True

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

问题描述

道歉,如果这已经被问过,但我徒劳地寻找一个答案,我的确切的问题。基本上,与Python 2.7,我已经运行一系列地理处理工具的计划,取决于什么是通过一系列的真/假变量的用户脚本例如调整reqested。

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: 

所以比出现假以外的任何值,以评估为真,这将不会是情况下,如果 X ==真或 x为true 。眼见为PEP 8强烈建议使用仅在如果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中的以下值是,如果的背景和其他逻辑语境假:

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




  • 数值等于0,如 0 0.0 -0.0

  • 空字符串: U''

  • 空容器(如列表,元组和字典)

  • 任何实现 __布尔__ (在Python3)返回 __非零__ (在Python2)返回 0

  • 任何不落实 __布尔__ (在Python3)或 __非零__ (在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

一个对象被认为是虚假的,如果任何这些应用,而真,否则,无论它实际上等于或相同与

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 是必然的对象之一,那么你可以放心地写如果x 。如果你已经安排了的真实性X 表示是否执行该操作,无论哪种类型,那么你可以放心地写如果X 。在这里你可以写,你应该preFER这样做,因为它的清洁阅读。

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 取值然后你在其中的一个两种情况,所以你不会写如果x为True 。最重要的是正确地记录的意义X ,因此,它反映了code使用的测试。

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程序员应知道怎样才算真正的,所以如果你只是文档,那么EX presses什么如果 X 是真实的运行功能你原来的code一样。记录它,运行功能,如果 x是真将有不同的含义,并不太常用的precisely因为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.

不过,如果你想要的code的情况下,以不同的表现,其中 X 是从那里的情况下一个空的容器,那么你会如果x不是没有写东西像

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.

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

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