'是'和'=='之间的区别 [英] Difference between 'is' and '=='
问题描述
的结果。什么时候正确使用''是''当
我们应该使用''==''?
非常感谢〜
mwql:嘿伙计们,这可能是一个愚蠢的问题,但我似乎无法找到
结果在线任何地方。什么时候使用''是'',什么时候我们应该使用''==''?
http://docs.python.org/ref/comparisons.html
-
$ b $bRenéPijlman
mwql写道:
嘿伙计们,这可能一个愚蠢的问题,但我似乎无法在网上找到
结果。什么时候使用''是'',什么时候我们应该使用''==''?
" is"就像id(obj1)== id(obj2)
100 + 1 == 101
真的
100 + 1是101
False
他们不喜欢没有相同的身份证明。 (想想id作为记忆地址。)
-
hilsen /问候丹麦Max M
http://www.mxm.dk/
IT疯狂科学
电话:+45 66 11 84 94
手机:+45 29 93 42 96
mwql写道:嘿伙计们,这可能是一个愚蠢的问题,但我似乎无法在任何地方找到
结果线上。什么时候使用''是'',什么时候我们应该使用''==''?
非常感谢〜
>
''==''是等于运算符。它用于测试两个对象是否为
''相等'。
''是''是身份运算符,它用于测试如果两个
名称/引用指向同一个对象。
a = {''a'':3}
b = {'''':3}
a == b
真实
a是b
错误
c = a
a是c
正确
两个字典a和b相等,但是单独的对象。
(在引擎盖下,Python使用''id''来确定身份)。
当你绑定另一个名字''c'时指向字典a,它们*是
相同的对象 - 所以a *是* c。
一个地方''是''运算符通常使用的是测试无。
你只有一个''无'的实例,所以
a是无
<
比
a ==无
(它只需要检查)身份不值。)
我希望有所帮助。
Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
Hey guys, this maybe a stupid question, but I can''t seem to find the
result anywhere online. When is the right time to use ''is'' and when
should we use ''==''?
Thanks alot~
mwql:Hey guys, this maybe a stupid question, but I can''t seem to find the
result anywhere online. When is the right time to use ''is'' and when
should we use ''==''?
http://docs.python.org/ref/comparisons.html
--
René Pijlman
mwql wrote:
Hey guys, this maybe a stupid question, but I can''t seem to find the
result anywhere online. When is the right time to use ''is'' and when
should we use ''==''?
"is" is like id(obj1) == id(obj2)
100+1 == 101 True
100+1 is 101
False
They don''t have the same id. (Think of id as memory adresses.)
--
hilsen/regards Max M, Denmark
http://www.mxm.dk/
IT''s Mad Science
Phone: +45 66 11 84 94
Mobile: +45 29 93 42 96
mwql wrote:Hey guys, this maybe a stupid question, but I can''t seem to find the
result anywhere online. When is the right time to use ''is'' and when
should we use ''==''?
Thanks alot~
''=='' is the equality operator. It is used to test if two objects are
''equal''.
''is'' is the identity operator, it is used to test if two
names/references point to the same object.
a = {''a'': 3}
b = {''a'': 3}
a == b
True
a is b
False
c = a
a is c
True
The two dictionaries a and b are equal, but are separate objects.
(Under the hood, Python uses ''id'' to determine identity).
When you bind another name ''c'' to point to dictionary a, they *are* the
same object - so a *is* c.
One place the ''is'' operator is commonly used is when testing for None.
You only ever have one instance of ''None'', so
a is None
is quicker than
a == None
(It only needs to check identity not value.)
I hope that helps.
Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
这篇关于'是'和'=='之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!