Python 简明教程

Python - Identity Operators

Python Identity Operators

同一性运算符比较对象以确定它们是否共享相同的内存并引用相同对象类型 ( data type )。

The identity operators compare the objects to determine whether they share the same memory and refer to the same object type (data type).

Python 提供了两个同一性运算符;我们已将其列出如下:

Python provided two identity operators; we have listed them as follows:

  1. 'is' Operator

  2. 'is not' Operator

Python 'is' Operator

如果两个操作数对象共享相同的内存位置,“ is ”运算符求值为 True。对象的内存位置可以通过“id()”函数获取。如果两个变量的“id()”相同,“is”运算符返回 True。

The 'is' operator evaluates to True if both the operand objects share the same memory location. The memory location of the object can be obtained by the "id()" function. If the "id()" of both variables is same, the "is" operator returns True.

Example of Python Identity 'is' Operator

a = [1, 2, 3, 4, 5]
b = [1, 2, 3, 4, 5]
c = a

# Comparing and printing return values
print(a is c)
print(a is b)

# Printing IDs of a, b, and c
print("id(a) : ", id(a))
print("id(b) : ", id(b))
print("id(c) : ", id(c))

它将生成以下 output

It will produce the following output

True
False
id(a) :  140114091859456
id(b) :  140114091906944
id(c) :  140114091859456

Python 'is not' Operator

如果两个操作数对象不共享相同的内存位置或两个操作数不是相同对象,“ is not ”运算符求值为 True。

The 'is not' operator evaluates to True if both the operand objects do not share the same memory location or both operands are not the same objects.

Example of Python Identity 'is not' Operator

a = [1, 2, 3, 4, 5]
b = [1, 2, 3, 4, 5]
c = a

# Comparing and printing return values
print(a is not c)
print(a is not b)

# Printing IDs of a, b, and c
print("id(a) : ", id(a))
print("id(b) : ", id(b))
print("id(c) : ", id(c))

它将生成以下 output

It will produce the following output

False
True
id(a) :  140559927442176
id(b) :  140559925598080
id(c) :  140559927442176

Python Identity Operators Examples with Explanations

Example 1

a="TutorialsPoint"
b=a
print ("id(a), id(b):", id(a), id(b))
print ("a is b:", a is b)
print ("b is not a:", b is not a)

它将生成以下 output

It will produce the following output

id(a), id(b): 2739311598832 2739311598832
a is b: True
b is not a: False

listtuple 对象的行为有所不同,乍一看可能很奇怪。在以下示例中,两个列表“a”和“b”包含相同的项。但它们的 id() 不同。

The list and tuple objects behave differently, which might look strange in the first instance. In the following example, two lists "a" and "b" contain same items. But their id() differs.

Example 2

a=[1,2,3]
b=[1,2,3]
print ("id(a), id(b):", id(a), id(b))
print ("a is b:", a is b)
print ("b is not a:", b is not a)

它将生成以下 output

It will produce the following output

id(a), id(b): 1552612704640 1552567805568
a is b: False
b is not a: True

这个列表或元组仅包含单个项的内存位置,而不包含项本身。因此,“a”包含某个位置中 10、20 和 30 整数对象的地址,其可能不同于“b”。

The list or tuple contains the memory locations of individual items only and not the items itself. Hence "a" contains the addresses of 10,20 and 30 integer objects in a certain location which may be different from that of "b".

Example 3

print (id(a[0]), id(a[1]), id(a[2]))
print (id(b[0]), id(b[1]), id(b[2]))

它将生成以下 output

It will produce the following output

140734682034984 140734682035016 140734682035048
140734682034984 140734682035016 140734682035048

由于“a”和“b”的两个不同位置,“ is ”运算符即使两个列表包含相同的数字也会返回 False。

Because of two different locations of "a" and "b", the "is" operator returns False even if the two lists contain same numbers.