<分区>
我运行这个 command在 python 控制台中:
为什么两个结果不同?
>>>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
标签 python
<分区>
我运行这个 command在 python 控制台中:
为什么两个结果不同?
>>>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
只有当对象是同一个对象时,结果才会为真。
==
如果对象的值相同则为真。
>>> 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
。
关于python - 比较两组字符串,返回不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42362855/
相关文章:
python - 在 Django 中,如何让 django-storages、boto 和 easy_thumbnail 正常工作?
python - python如何测试从数据库中提取的一些纯文本中的二进制数据
python - 如何在脚本中使用 IPython 魔法来自动重新加载模块?
php - 使用外部 API 访问时,Python Bottle API 无 Json 响应
android - 使用 kivy 在 Android 和 IOS 中播放音频(mp3)