为什么“是”在这两种情况下工作方式不同

时间:2020-06-29 16:19:02

标签: python-3.x equality re

我的理解是,“ is”关键字检查两个对象实际上是同一对象。

str类型是不可变的,可以使用“ is”进行比较,因为(我假设)它们始终由相同的基础对象表示。例如

"abc" is "abc" # True
a = "abc"; a == "abc" # True
lst = ["abc", 1, 2]; lst[0] is "abc" # True

但是(我知道任意示例),如果我做了这样的事情:

string_a = "abc_123"
string_b = re.search("abc", string_a).group() # gets "abc"
cls = type(string_b). # Says its a str type
string_b == "abc"  # Returns True & Shows me that it is "abc"
string_b is "abc"  # Returns False.

当string_b的类型为str且等于“ abc”时,为什么最后一行返回False?

0 个答案:

没有答案