两个字符串变量被设置为相同的值。s1 == s2总是返回True,但s1 = s2有时返回False。

如果我打开我的Python解释器,并做同样的比较,它成功了:

>>> s1 = 'text'
>>> s2 = 'text'
>>> s1 is s2
True

为什么会这样?


当前回答

最后需要注意的是,你可以使用sys.intern函数来确保你得到的是同一个字符串的引用:

>>> from sys import intern
>>> a = intern('a')
>>> a2 = intern('a')
>>> a is a2
True

正如在前面的回答中指出的,您不应该使用is来确定字符串的相等性。但这可能有助于了解您是否有一些奇怪的要求使用is。

注意,intern函数曾经是Python 2的内置函数,但在Python 3中被移到了sys模块中。

其他回答

实际上,is操作符检查是否相同,而==操作符检查是否相等。

从语言参考:

Types affect almost all aspects of object behavior. Even the importance of object identity is affected in some sense: for immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value, while for mutable objects this is not allowed. E.g., after a = 1; b = 1, a and b may or may not refer to the same object with the value one, depending on the implementation, but after c = []; d = [], c and d are guaranteed to refer to two different, unique, newly created empty lists. (Note that c = d = [] assigns the same object to both c and d.)

因此,从上面的语句中,我们可以推断出字符串是不可变类型,当使用"is"检查时可能会失败,而当使用"is"检查时可能会成功。

同样适用于int和tuple,它们也是不可变类型。

is关键字是测试对象身份,而==是值比较。

如果使用is,当且仅当对象是同一对象时,结果将为真。但是,当对象的值相同时,==将为真。

将比较内存位置。它用于对象级比较。

==将比较程序中的变量。它用于在值级别上进行检查。

是否检查地址级别等价性

==检查值级别是否等价

这是一个边注,但在惯用的Python中,你经常会看到这样的东西:

if x is None:
    # Some clauses

这是安全的,因为保证有一个Null对象的实例(即None)。

最后需要注意的是,你可以使用sys.intern函数来确保你得到的是同一个字符串的引用:

>>> from sys import intern
>>> a = intern('a')
>>> a2 = intern('a')
>>> a is a2
True

正如在前面的回答中指出的,您不应该使用is来确定字符串的相等性。但这可能有助于了解您是否有一些奇怪的要求使用is。

注意,intern函数曾经是Python 2的内置函数,但在Python 3中被移到了sys模块中。