两个字符串变量被设置为相同的值。s1 == s2总是返回True,但s1 = s2有时返回False。
如果我打开我的Python解释器,并做同样的比较,它成功了:
>>> s1 = 'text'
>>> s2 = 'text'
>>> s1 is s2
True
为什么会这样?
两个字符串变量被设置为相同的值。s1 == s2总是返回True,但s1 = s2有时返回False。
如果我打开我的Python解释器,并做同样的比较,它成功了:
>>> s1 = 'text'
>>> s2 = 'text'
>>> s1 is s2
True
为什么会这样?
当前回答
这里的其他答案是正确的:is用于恒等比较,而==用于相等比较。由于您关心的是相等性(两个字符串应该包含相同的字符),在这种情况下,is操作符是错误的,您应该使用==代替。
原因是交互工作的(大多数)字符串字面量是默认的。从维基百科:
Interned strings speed up string comparisons, which are sometimes a performance bottleneck in applications (such as compilers and dynamic programming language runtimes) that rely heavily on hash tables with string keys. Without interning, checking that two different strings are equal involves examining every character of both strings. This is slow for several reasons: it is inherently O(n) in the length of the strings; it typically requires reads from several regions of memory, which take time; and the reads fills up the processor cache, meaning there is less cache available for other needs. With interned strings, a simple object identity test suffices after the original intern operation; this is typically implemented as a pointer equality test, normally just a single machine instruction with no memory reference at all.
因此,当你在程序中有两个具有相同值的字符串字面量(字面上输入到程序源代码中的单词,被引号包围)时,Python编译器将自动对字符串进行替换,使它们都存储在相同的内存位置。(请注意,这种情况并不总是发生,发生这种情况的规则相当复杂,所以请不要在生产代码中依赖这种行为!)
由于在交互式会话中,两个字符串实际上存储在相同的内存位置,因此它们具有相同的标识,因此is操作符可以正常工作。但是如果您通过其他方法构造一个字符串(即使该字符串包含完全相同的字符),那么字符串可能是相等的,但它不是相同的字符串——也就是说,它具有不同的标识,因为它存储在内存中的不同位置。
其他回答
根据我有限的Python使用经验,is用于比较两个对象,以确定它们是否是相同的对象,而不是具有相同值的两个不同对象。==用于确定两个值是否相同。
这里有一个很好的例子:
>>> s1 = u'public'
>>> s2 = 'public'
>>> s1 is s2
False
>>> s1 == s2
True
s1是Unicode字符串,s2是普通字符串。它们不是相同的类型,但它们的值相同。
最后需要注意的是,你可以使用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是同一性测试,==是相等性测试。在你的代码中发生的事情将在解释器中模拟,就像这样:
>>> a = 'pub'
>>> b = ''.join(['p', 'u', 'b'])
>>> a == b
True
>>> a is b
False
所以,难怪它们不一样,对吧?
换句话说,a等于b等于id(a) == id(b)
这里的其他答案是正确的:is用于恒等比较,而==用于相等比较。由于您关心的是相等性(两个字符串应该包含相同的字符),在这种情况下,is操作符是错误的,您应该使用==代替。
原因是交互工作的(大多数)字符串字面量是默认的。从维基百科:
Interned strings speed up string comparisons, which are sometimes a performance bottleneck in applications (such as compilers and dynamic programming language runtimes) that rely heavily on hash tables with string keys. Without interning, checking that two different strings are equal involves examining every character of both strings. This is slow for several reasons: it is inherently O(n) in the length of the strings; it typically requires reads from several regions of memory, which take time; and the reads fills up the processor cache, meaning there is less cache available for other needs. With interned strings, a simple object identity test suffices after the original intern operation; this is typically implemented as a pointer equality test, normally just a single machine instruction with no memory reference at all.
因此,当你在程序中有两个具有相同值的字符串字面量(字面上输入到程序源代码中的单词,被引号包围)时,Python编译器将自动对字符串进行替换,使它们都存储在相同的内存位置。(请注意,这种情况并不总是发生,发生这种情况的规则相当复杂,所以请不要在生产代码中依赖这种行为!)
由于在交互式会话中,两个字符串实际上存储在相同的内存位置,因此它们具有相同的标识,因此is操作符可以正常工作。但是如果您通过其他方法构造一个字符串(即使该字符串包含完全相同的字符),那么字符串可能是相等的,但它不是相同的字符串——也就是说,它具有不同的标识,因为它存储在内存中的不同位置。