首先看几个例子:
# -*-coding=utf-8 -*-
a="Hello Python"
b="Hello Python"
print (a is b) #结果是:True
print (a == b) #结果是:True
a=[1,2,3]
b=[1,2,3]
print (a is b) #结果是:False
print (a == b) #结果是:True
a=[1,2,3]
b=a
print (a is b) #结果是:True
print (a == b) #结果是:True
运行结果:
True
True
False
True
True
True
is 表示同一性测试
== 表示比较
程序在查检a is b时,表示id(a)==id(b),而检查a==b时,调用对象__eq__()方法,相当于a.__eq__(b)
a="Hello Python"
b="Hello Python"
print (id(a),id(b))
a=[1,2,3]
b=[1,2,3]
print (id(a),id(b))
a=[1,2,3]
b=a
print (id(a),id(b))
运行结果:
(82999696L, 82999696L)
(83640456L, 83756168L)
(83757576L, 83757576L)
转载于:https://blog.51cto.com/guoshiwei/2114572