惊喜使用'is'运算符 [英] Surprise using the 'is' operator
问题描述
我认为''是''运算符用于标识相同的对象,
而''==''运算符检查了相等性。好吧,我有一个惊喜
这里:
IDLE 1.1.3
>> a = 10
b = a
a是b
True
>> a == b
True
>> c = 10
a == c
True
>> a是c
True
>>& GT;
我不希望最后一个语句返回True!
我是什么缺少?
谢谢
Tony
codefire写道:
我认为''是''运算符用于识别相同的对象,
而''==''操作员检查了相等好吧,我有一个惊喜
这里:
IDLE 1.1.3
>>> a = 10
b = a
a是b
True
>>> a == b
True
>>> c = 10
a == c
True
>>> a是c
True
>>> ;>
我不希望最后一个语句返回True!
我缺少什么?
蟒蛇最严重的死马之一 - 小整数是由翻译缓存的
。也许。因此上述结果。
谷歌就此进行了大量讨论。
Diez
codefireaécrit:
我认为''是''运算符用于识别相同的对象,
而'' '==''运算符检查了相等性。好吧,我有一个惊喜
这里:
IDLE 1.1.3
>>> a = 10
b = a
a是b
True
>>> a == b
True
>>> c = 10
a == c
True
>>> a是c
True
我不希望最后一个语句返回True!
答案是:为什么不呢?整数是否重要?永远不要使用
" is"关于整数,总是==(对于字符串也是如此)
哈哈!
好的谢谢你们。
我只是试图检查对象是否相同(对象),没有
知道整数是一个特例。
谢谢,
Tony
I thought the ''is'' operator was used to identify identical objects,
whereas the ''=='' operator checked equality. Well, I got a surprise
here:
IDLE 1.1.3
>>a = 10
b = a
a is b
True
>>a == b
True
>>c = 10
a == c
True
>>a is c
True
>>>
I was NOT expecting the last statement to return True!
What am I missing?
Thanks
Tony
codefire wrote:
I thought the ''is'' operator was used to identify identical objects,
whereas the ''=='' operator checked equality. Well, I got a surprise
here:
IDLE 1.1.3>>>a = 10
b = a
a is b
True
>>>a == b
True
>>>c = 10
a == c
True
>>>a is c
True
>>>>
I was NOT expecting the last statement to return True!
What am I missing?One of the most severely beaten dead horses of python - small integers are
cached by the interpreter. Maybe. Thus the above results.
Google for a bazillion discussions on this.
Diez
codefire a écrit :I thought the ''is'' operator was used to identify identical objects,
whereas the ''=='' operator checked equality. Well, I got a surprise
here:
IDLE 1.1.3>>>a = 10
b = a
a is b
True
>>>a == b
True
>>>c = 10
a == c
True
>>>a is c
True
I was NOT expecting the last statement to return True!The answer is : Why not? Does it even matter for integers? Never use
"is" on integers, always == ( and on strings too for that matter )
Haha!
OK thanks guys.
I was just trying to check if objects were the same (object), didn''t
know Integers were a special case.
Thanks,
Tony
这篇关于惊喜使用'is'运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!