Python“是”声明:发生了什么?

时间:2022-12-03 14:37:04

I was quite surprised when

我很惊讶

[] is not []

evaluated to True.

评估为True。

What is happening in this code? What really not and is statements are doing?

这段代码发生了什么?什么不是,而且声明正在做什么?

7 个解决方案

#1


53  

a is not b is a special operator which is equivalent to not a is b.

a不是b是一个特殊的算子,相当于不是a是b。

The operator a is b returns True if a and b are bound to the same object, otherwise False. When you create two empty lists you get two different objects, so is returns False (and therefore is not returns True).

如果a和b绑定到同一个对象,则运算符a是b返回True,否则返回False。当您创建两个空列表时,您将获得两个不同的对象,因此返回False(因此不返回True)。

#2


29  

is is the identity comparison.

是身份比较。

== is the equality comparison.

==是平等比较。

Your statement is making two different lists and checking if they are the same instance, which they are not. If you use == it will return true and because they are both empty lists.

您的语句正在制作两个不同的列表,并检查它们是否是同一个实例,而不是它们。如果你使用==它将返回true,因为它们都是空列表。

#3


22  

The best way to describe WHY that happens is this:

描述为什么会发生这种情况的最佳方式是:

Here is your example

这是你的例子

>>> x = []
>>> y = []
>>> print(x is y)
... False

x and y are actually two different lists, so if you add something to x, it does not appear in y

x和y实际上是两个不同的列表,所以如果你向x添加一些东西,它就不会出现在y中

>>> x.append(1)
>>> print(x)
... [1]
>>> print(y)
... []

So how do we make (x is y) evaluate true?

那么我们如何使(x是y)评估为真呢?

>>> x = []
>>> y = x
>>> print(x is y)
... True

>>> x.append(10)

>>> print(x)
... [10]
>>> print(y)
... [10]

>>> print(x is y)
... True

if you want to see if two lists have the same contents...

如果你想看看两个列表是否有相同的内容......

>>> x = []
>>> y = []
>>> print(x == y)
... True

>>> x.append(21)

>>> print(x)
... [21]
>>> print(y)
... []

>>> print(x == y)
... False

>>> y = [21]
>>> print(x == y)
... True

#4


8  

is means is same instance. It evaluates to true if the variables on either side of the operator point to the same object and false otherwise.

是手段是同一个实例。如果运算符两侧的变量指向同一对象,则计算结果为true,否则为false。

Reference, near the bottom.

参考,靠近底部。

#5


3  

is checks for identity. [] and [] are two different (but equivalent) lists. If you want to check if both the lists are empty you can use their truth value (false for empty strings, collections, and zeros).

是检查身份。 []和[]是两个不同(但等效)的列表。如果要检查列表是否都为空,则可以使用其真值(对于空字符串,集合和零,为false)。

if not ([] and []):
   print 'Spanish Inquisition'

the only time that is is guaranteed to return True is for singletons such as None. Like the Highlander, there can be only one instance of None in your program - every time you return None it's the very same "thing" as the none referred to if you type print None.

保证返回True的唯一时间是单身,例如None。与Highlander一样,程序中只能有一个None实例 - 每次返回None时,如果键入print None,则与“none”相同。

[], OTOH, is not guaranteed to be anything except an empty list and evaluate to False in a boolean context.

除了空列表之外,[],OTOH不能保证是任何东西,并且在布尔上下文中求值为False。

#6


2  

I know I am posting to a pretty old post. however, this might help someone stumble upon this like me.

我知道我发帖到一个很老的帖子。然而,这可能有助于有人像我一样偶然发现这一点。

"is" checks, whether a memory address is same or not, while "==" check if the value is same or not. Would be much clear from the following example

“是”检查,内存地址是否相同,而“==”检查值是否相同。从以下示例中可以清楚地看出

let's first talk about immutable objects, as it's easy to understand

让我们先谈谈不可变对象,因为它很容易理解

# could be any immutable object
immutable_a = 10
immutable_b = 10

# prints address of a and b variable
print "address of a is %s" % id(immutable_a)
print "address of a is %s" % id(immutable_b)

# as both addresses is same, following shall be true
print immutable_a is immutable_b

# as the values are also same, following shall be true as well
print immutable_a == immutable_b

now let's talk about mutable objects

现在让我们谈谈可变对象

# could be any mutable object
mutable_a = [10]
mutable_b = [10]

# prints address of a and b variable
print "address of mutable_a is %s" % id(mutable_a)
print "address of mutable_b is %s" % id(mutable_b)

# as addresses are not same, following shall be false
print mutable_a is mutable_b

# as the values are same, following shall be true
print mutable_a == mutable_b

#7


0  

@Jiaaro is right. Using is with immutable data types is dangerous because it is not predictable because of Pythons interpreter optimization.

@Jiaaro是对的。使用is与不可变数据类型是危险的,因为由于Pythons解释器优化它是不可预测的。

See this example:

看这个例子:

10 * "a" is 10 * "a"    # True
100 * "a" is 100 * "a"  # False

In the second line it is faster to create a new object with a new id for the interpreter. So use the is operator only with mutable types.

在第二行中,使用解释器的新id创建新对象会更快。因此,仅将is运算符与可变类型一起使用。

#1


53  

a is not b is a special operator which is equivalent to not a is b.

a不是b是一个特殊的算子,相当于不是a是b。

The operator a is b returns True if a and b are bound to the same object, otherwise False. When you create two empty lists you get two different objects, so is returns False (and therefore is not returns True).

如果a和b绑定到同一个对象,则运算符a是b返回True,否则返回False。当您创建两个空列表时,您将获得两个不同的对象,因此返回False(因此不返回True)。

#2


29  

is is the identity comparison.

是身份比较。

== is the equality comparison.

==是平等比较。

Your statement is making two different lists and checking if they are the same instance, which they are not. If you use == it will return true and because they are both empty lists.

您的语句正在制作两个不同的列表,并检查它们是否是同一个实例,而不是它们。如果你使用==它将返回true,因为它们都是空列表。

#3


22  

The best way to describe WHY that happens is this:

描述为什么会发生这种情况的最佳方式是:

Here is your example

这是你的例子

>>> x = []
>>> y = []
>>> print(x is y)
... False

x and y are actually two different lists, so if you add something to x, it does not appear in y

x和y实际上是两个不同的列表,所以如果你向x添加一些东西,它就不会出现在y中

>>> x.append(1)
>>> print(x)
... [1]
>>> print(y)
... []

So how do we make (x is y) evaluate true?

那么我们如何使(x是y)评估为真呢?

>>> x = []
>>> y = x
>>> print(x is y)
... True

>>> x.append(10)

>>> print(x)
... [10]
>>> print(y)
... [10]

>>> print(x is y)
... True

if you want to see if two lists have the same contents...

如果你想看看两个列表是否有相同的内容......

>>> x = []
>>> y = []
>>> print(x == y)
... True

>>> x.append(21)

>>> print(x)
... [21]
>>> print(y)
... []

>>> print(x == y)
... False

>>> y = [21]
>>> print(x == y)
... True

#4


8  

is means is same instance. It evaluates to true if the variables on either side of the operator point to the same object and false otherwise.

是手段是同一个实例。如果运算符两侧的变量指向同一对象,则计算结果为true,否则为false。

Reference, near the bottom.

参考,靠近底部。

#5


3  

is checks for identity. [] and [] are two different (but equivalent) lists. If you want to check if both the lists are empty you can use their truth value (false for empty strings, collections, and zeros).

是检查身份。 []和[]是两个不同(但等效)的列表。如果要检查列表是否都为空,则可以使用其真值(对于空字符串,集合和零,为false)。

if not ([] and []):
   print 'Spanish Inquisition'

the only time that is is guaranteed to return True is for singletons such as None. Like the Highlander, there can be only one instance of None in your program - every time you return None it's the very same "thing" as the none referred to if you type print None.

保证返回True的唯一时间是单身,例如None。与Highlander一样,程序中只能有一个None实例 - 每次返回None时,如果键入print None,则与“none”相同。

[], OTOH, is not guaranteed to be anything except an empty list and evaluate to False in a boolean context.

除了空列表之外,[],OTOH不能保证是任何东西,并且在布尔上下文中求值为False。

#6


2  

I know I am posting to a pretty old post. however, this might help someone stumble upon this like me.

我知道我发帖到一个很老的帖子。然而,这可能有助于有人像我一样偶然发现这一点。

"is" checks, whether a memory address is same or not, while "==" check if the value is same or not. Would be much clear from the following example

“是”检查,内存地址是否相同,而“==”检查值是否相同。从以下示例中可以清楚地看出

let's first talk about immutable objects, as it's easy to understand

让我们先谈谈不可变对象,因为它很容易理解

# could be any immutable object
immutable_a = 10
immutable_b = 10

# prints address of a and b variable
print "address of a is %s" % id(immutable_a)
print "address of a is %s" % id(immutable_b)

# as both addresses is same, following shall be true
print immutable_a is immutable_b

# as the values are also same, following shall be true as well
print immutable_a == immutable_b

now let's talk about mutable objects

现在让我们谈谈可变对象

# could be any mutable object
mutable_a = [10]
mutable_b = [10]

# prints address of a and b variable
print "address of mutable_a is %s" % id(mutable_a)
print "address of mutable_b is %s" % id(mutable_b)

# as addresses are not same, following shall be false
print mutable_a is mutable_b

# as the values are same, following shall be true
print mutable_a == mutable_b

#7


0  

@Jiaaro is right. Using is with immutable data types is dangerous because it is not predictable because of Pythons interpreter optimization.

@Jiaaro是对的。使用is与不可变数据类型是危险的,因为由于Pythons解释器优化它是不可预测的。

See this example:

看这个例子:

10 * "a" is 10 * "a"    # True
100 * "a" is 100 * "a"  # False

In the second line it is faster to create a new object with a new id for the interpreter. So use the is operator only with mutable types.

在第二行中,使用解释器的新id创建新对象会更快。因此,仅将is运算符与可变类型一起使用。