我有以下python代码来进行字符串比较:
>>> mf="moo foo"
>>> mf[0:3]
'moo'
>>> mf[0:3] is "moo"
False
>>> mf[0:3] == "moo"
True
>>> str(mf[0:3]) is "moo"
False
所以double equals操作符返回true,但是pythonis
操作符返回false。这一定是因为它们不是同一个对象。你知道吗
用str[n:m]
获得的字符序列和同一字符串的文字表示('moofoo'
)之间有什么技术区别?为什么它们不是同一个物体?你知道吗
is
测试相等性以及相同的对象ID(在CPython中,这是内存中对象的地址)。str(...)
而切片创建了一个浅拷贝,因此is
测试失败。你知道吗==
测试相等的值。is
测试对象标识。你知道吗引自Python's Mailing List
相关问题 更多 >
编程相关推荐