Python:对象相等
看代码:
s1 = []
s2 = []
print s1 is s2 # False
print s1 == s2 # True
is
判断引用相等性
==
判断对象相等性(最好只用于数比较)?
Python 3.X 版本中没有 cmp
函数(用于字典元素相等性比较),如果你需要实现比较功能,需要引入 operator
模块,适合任何对象:
import operator
operator.lt(a, b)
operator.le(a, b)
operator.eq(a, b)
operator.ne(a, b)
operator.ge(a, b)
operator.gt(a, b)
operator.__lt__(a, b)
operator.__le__(a, b)
operator.__eq__(a, b)
operator.__ne__(a, b)
operator.__ge__(a, b)
operator.__gt__(a, b)