Python对象比较:“is”与“==”
“is”和“==”都用于Python中的对象比较。运算符“==”比较两个对象的值,而“is”检查两个对象是否相同(换句话说,两个引用相同的对象)。
# Python program to demonstrate working of
# "=="
# Two different objects having same values
x1 = [10, 20, 30]
x2 = [10, 20, 30]
# Comparison using "==" operator
if x1 == x2:
print("Yes")
else:
print("No")
输出:
Yes
“==”运算符并没有告诉我们 x1 和 x2 是否实际上指的是同一个对象。为此,我们使用“is”。
# Python program to demonstrate working of
# "is"
# Two different objects having same values
x1 = [10, 20, 30]
x2 = [10, 20, 30]
# We get "No" here
if x1 is x2:
print("Yes")
else:
print("No")
# It creates another reference x3 to same list.
x3 = x1
# So we get "Yes" here
if x1 is x3:
print("Yes")
else:
print("No")
# "==" would also produce yes anyway
if x1 == x3:
print("Yes")
else:
print("No")
输出:
No
Yes
Yes
x1 = [10, 20, 30]
# Here a new list x2 is created using x1
x2 = list(x1)
# The "==" operator would produce "Yes"
if x1 == x2:
print("Yes")
else:
print("No")
# But "is" operator would produce "No"
if x1 is x2:
print("Yes")
else:
print("No")
输出:
Yes
No
结论:
- 如果两个变量指向同一个对象,“is”返回 True。
- 如果两个变量具有相同的值(或内容),“==”返回 True。