我正在尝试下面的代码:
为什么最后的表达式会出现false?
x = 'asd'
y = 'asd'
z = input() #write here string 'asd'. For Python 2.x use raw_input()
x == y # True.
x is y # True.
x == z # True.
x is z # False.
为什么最后的表达式会出现false?
is
操作符在比较带有空格的字符串时表现不同。 - Ashwini Chaudharyis
在cpython中有时候作为实现细节而巧合地起作用。输入数据并不是特别特殊的事情;应该明确的是永远不要使用is
。 - Wooble