简体   繁体   English

(x不是None)和(y不是None)和(x和y)之间的python区别不是None

[英]python difference between (x is not None) and (y is not None) and (x and y) is not None

The following conditions give the same results. 以下条件给出相同的结果。 Are there any performance (or any other) differences between them ? 两者之间是否有任何性能差异(或其他差异)?

1. 1。

if (x != None) and (y != None):
    # Proceed

2. 2。

if (x and y) is not None:
    # Proceed
In [44]: x = False

In [45]: y = None

In [46]: (x != None) and (y != None)
Out[46]: False

In [47]: (x and y) is not None
Out[47]: True

By the way, when testing if something is None, it is better to test 顺便说一句,当测试是否为None时,最好进行测试

if x is None

rather than 而不是

if x == None

The result can be different: 结果可能会有所不同:

class Egalitarian(object):
    def __eq__(self, other):
        return True

x = Egalitarian()

print(x == None)
# True

print(x is None)
# False

A programmer's wife asks him: do you want sausage or bacon for breakfast? 程序员的妻子问他:早餐要吃香肠还是培根? - Yes, he responds. -是的,他回答。 (a true story). (一个真实的故事)。

When using boolean constructs in everyday speaking we tend to skip common parts so that X(Y) @ X(Z) becomes X @ (Y,Z) : 在日常工作中使用布尔构造时,我们倾向于跳过通用部分,以使X(Y) @ X(Z)变为X @ (Y,Z)

Her bunny is happy and her hamster is happy (1)

is the same as 是相同的

Her bunny and her hamster are happy

In programming, however, we cannot contract things like that. 但是,在编程中,我们不能像这样收缩事情。 The first statement 第一句话

bunny == happy and hamster == happy

will be true if both pets are fine. 如果两只宠物都很好,那将是正确的。 The second phrase translates literally to this: 第二句话直译为:

(bunny and hamster) == happy 

here, (bunny and hamster) evaluates to 在这里, (bunny and hamster)计算结果为

- a falsy value, if she's got no bunny
- the hamster otherwise

So (bunny and hamster) == happy actually reads as: 所以(bunny and hamster) == happy是:

She's got a bunny and her hamster is happy

which is quite different from the statement (1) 与陈述(1)完全不同

In python: 在python中:

>>> happy = 1
>>> sad = 2
>>> bunny = sad
>>> hamster = happy
>>> bunny == happy and hamster == happy
False
>>> (bunny and hamster) == happy
True
>>> 

You need to understand what it is you are doing to understand the real differences, you might think the following is a simple statement: 您需要了解正在做的事情才能了解真正的差异,您可能会认为以下内容很简单:

>>> if (x and y) is not None:

It is not a simple conditional statement. 这不是一个简单的条件语句。 Here, you are first evaluating (x and y) which will return the last Truthy value it finds, or the first Falsey value it finds, and then compare that to is not None . 在这里,你第一次评估(x and y)将返回最后Truthy价值发现或找到的第一个Falsey值,然后比较,为is not None Which will yield sometimes unexpected results. 有时会产生意想不到的结果。 (Consult The Peculiar Nature of and and or ) (请咨询andor的特殊性质

If you want to check to make sure that both x and y are not None you should do something like the following: 如果要检查,以确保这两个 xyNone你应该做类似如下:

>>> if all(var is not None for var in [x, y]):

Which is almost the same as: 几乎与以下内容相同:

>>> if x is not None and y is not None:

Only a bit easier to read, and more robust (if you add more variables to check), 仅一点点就更易于阅读,并且更健壮(如果要添加更多变量来检查),


Either way, the difference between your two statements are that the first one will actually do what you expect, but you should be using is not instead of != when it comes to None . 无论哪种方式,您的两个语句之间的区别在于,第一个语句实际上将按照您的期望进行操作,但是当涉及到None时,您应该使用is not不是!= And the second statement might work sometimes, but is doing something that you probably were not expecting. 第二条语句有时可能会起作用,但是正在做您可能没想到的事情。

Usually 通常

if x and y:
    #Proceed

is good enough in the Python world. 在Python世界中足够好。

Here I says good enough because in most cases a function should not return False and None while they have different meanings in the function logic. 我在这里说的足够好,因为在大多数情况下,函数在函数逻辑中具有不同含义时不应返回False和None。

For those downvoters, seriously will you write the following codes in real world? 对于那些讨厌的人,您会认真地在现实世界中编写以下代码吗?

if x is None:
    #xxx
elif x == False:
    #xxxx
else:
    #xxx

I just want to point out that the both example are not pythonic and should be avoided. 我只想指出,两个示例都不是pythonic的,应该避免。

"is" and "is not" are fast as they check identity (ie comparing pointers/references) “是”和“不是”在检查身份(即比较指针/引用)时很快

The other comparisons and logical operators will inspect the object's values (or truthiness). 其他比较和逻辑运算符将检查对象的值(或真实性)。

Generally to check for "None" values, you therefore want to use "is"/"is not". 通常,要检查“无”值,因此要使用“是” /“不是”。

Personally I find that relying on the behaviour of None values in an expression is bad form (things that would often trigger NullPointerException or similar errors in many other languages), so while I'm not completely sure what is you are trying to achieve, generally similar code I write looks like: 我个人发现,依靠表达式中None值的行为是错误的形式(通常会触发NullPointerException或许多其他语言的类似错误的东西),因此尽管我不确定自己要达到的目标,但通常我写的类似代码看起来像:

if x is not None and y is not None:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM