无法弄清楚为什么两个值 "look" 相同但不相等

can't figure out why two values "look" the same but aren't equal

我正在 Python 2.6.6 中的 PyCharm 上编写应用程序,但没有得到我期望的输出:

if VAR1 != row2:
    print "Status 1: %s" %VAR1
    print "Status 2: %s" %row2
    print "%s != %s" % (VAR1, row2)

输出:

Status 1: 3
Status 2: 3
3 != (3L,)

有人知道这里发生了什么吗?

row2 是一个有 1 个元素的元组。 % 字符串的格式可以与单个值或一个或多个值的元组一起使用。

print "Status 2: %s" %row2 中,元组被解包并使用唯一元素。在 print "%s != %s" % (VAR1, row2) 中,您已将元组 row2 打包到另一个元组中,因此第二个 %s 显示该元组的表示形式,例如(3,).