Python:为什么运算符"is"和"=="有时可以互换为字符串?

Edu*_*scu 5 python operators python-2.7

可能重复:
Python中的字符串比较:is = = =
Python string interning 为什么使用'=='或'is'比较Python中的字符串
有时会产生不同的结果?

我偶然使用is==字符串交替使用,但我发现并不总是一样的.

>>> Folder = "locales/"
>>> Folder2 = "locales/"
>>> Folder is Folder2
False
>>> Folder == Folder2
True
>>> File = "file"
>>> File2 = "file"
>>> File is File2
True
>>> File == File2
True
>>> 
Run Code Online (Sandbox Code Playgroud)

为什么在一种情况下操作符可以互换而在另一种情况下不可以?

Dan*_*man 12

短字符串是为了提高效率而实现的,因此将引用相同的对象,因此is也是如此.

这是CPython中的一个实现细节,绝对不能依赖它.