问题:“ is None”和“ == None”有什么区别?

我最近遇到了这种语法,我不知道有什么区别。

如果有人可以告诉我与众不同,我将不胜感激。

I recently came across this syntax, I am unaware of the difference.

I would appreciate it if someone could tell me the difference.


回答 0

答案在这里解释。

报价:

一个类可以自由选择以任何方式实现比较,并且可以选择与None进行比较意味着某种意义(这实际上是有道理的;如果有人告诉您从头开始实现None对象,那么您将如何获得它来比较True?反对自己?)。

实际上,由于自定义比较运算符很少见,因此差异不大。但是您应该使用is None一般规则。

The answer is explained here.

To quote:

A class is free to implement comparison any way it chooses, and it can choose to make comparison against None mean something (which actually makes sense; if someone told you to implement the None object from scratch, how else would you get it to compare True against itself?).

Practically-speaking, there is not much difference since custom comparison operators are rare. But you should use is None as a general rule.


回答 1

class Foo:
    def __eq__(self,other):
        return True
foo=Foo()

print(foo==None)
# True

print(foo is None)
# False
class Foo:
    def __eq__(self,other):
        return True
foo=Foo()

print(foo==None)
# True

print(foo is None)
# False

回答 2

在这种情况下,它们是相同的。None是单例对象(只有一个存在None)。

is 检查对象是否是同一对象,而==只是检查它们是否等效。

例如:

p = [1]
q = [1]
p is q # False because they are not the same actual object
p == q # True because they are equivalent

但是由于只有一个None,它们将始终相同,is并将返回True。

p = None
q = None
p is q # True because they are both pointing to the same "None"

In this case, they are the same. None is a singleton object (there only ever exists one None).

is checks to see if the object is the same object, while == just checks if they are equivalent.

For example:

p = [1]
q = [1]
p is q # False because they are not the same actual object
p == q # True because they are equivalent

But since there is only one None, they will always be the same, and is will return True.

p = None
q = None
p is q # True because they are both pointing to the same "None"

回答 3

如果您使用numpy,

if np.zeros(3)==None: pass

numpy进行元素比较时会给你错误

If you use numpy,

if np.zeros(3)==None: pass

will give you error when numpy does elementwise comparison


回答 4

这取决于您要与“无”进行比较。一些类具有自定义比较方法,这些方法与== None区别对待is None

特别是的输出a == None 甚至不必是布尔值!-错误的常见原因。

对于特定示例,请使用numpy数组,其中==逐元素实现比较:

import numpy as np
a = np.zeros(3) # now a is array([0., 0., 0.])
a == None #compares elementwise, outputs array([False, False, False]), i.e. not boolean!!!
a is None #compares object to object, outputs False

It depends on what you are comparing to None. Some classes have custom comparison methods that treat == None differently from is None.

In particular the output of a == None does not even have to be boolean !! – a frequent cause of bugs.

For a specific example take a numpy array where the == comparison is implemented elementwise:

import numpy as np
a = np.zeros(3) # now a is array([0., 0., 0.])
a == None #compares elementwise, outputs array([False, False, False]), i.e. not boolean!!!
a is None #compares object to object, outputs False

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。