有人能解釋我在python 2.6.6上這個奇怪的結果嗎?Python中的奇怪結果
>>> a = "xx"
>>> b = "xx"
>>> a.__hash__() == b.__hash__()
True
>>> a is b
True # ok.. was just to be sure
>>> a = "x" * 2
>>> b = "x" * 2
>>> a.__hash__() == b.__hash__()
True
>>> a is b
True # yeah.. looks ok so far !
>>> n = 2
>>> a = "x" * n
>>> b = "x" * n
>>> a.__hash__() == b.__hash__()
True # still okay..
>>> a is b
False # hey! What the F... ?
人們知道'is'是什麼,但不知道它與'=='有什麼不同? – delnan 2010-11-04 15:56:38
[Python'=='vs'可能重複'比較字符串','有時會失敗,爲什麼?](http://stackoverflow.com/questions/1504717/python-vs-is-comparing-strings-is -fails-sometimes-why) – SilentGhost 2010-11-04 16:02:50
@SilentGhost:不完全是,因爲這涉及編譯器意外插入字符串時的主題。 – 2010-11-04 16:06:52