我在Python控制台中运行了这个命令:
为什么这2个结果不同?
>>>S1 = 'HelloWorld'
>>>S2 = 'HelloWorld'
>>>S1 is S2
True
>>>S1 = 'Hello World'
>>>S2 = 'Hello World'
>>>S1 is S2
False ---------i think the result is True,why it is False
is
只有在对象相同时才返回 true。
==
只有在对象的值相同时才返回 true。
>>> S1 = 'HelloWorld'
>>> print id(S1)
4457604464
>>> S2 = 'HelloWorld'
>>> print id(S2)
4457604464
>>> S1 is S2
True
S1
和 S2
是同一个对象。它们具有相同的内存位置。所以 S1
就是 S2
。>>> S1 = 'Hello World'
>>> S2 = 'Hello World'
>>> print id(S1)
4457604320
>>> print id(S2)
4457604272
>>> S1 is S2
False
现在,它们是不同的对象,所以S1
不等于S2
。
S1
和 S2
不是同一个对象。 - juanpa.arrivillaga
true
。 - Chanda Korat