什么是关键字“是”对于? [英] what is the keyword "is" for?

查看:64
本文介绍了什么是关键字“是”对于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对关键字is感到困惑。和==等号,似乎他们可以在某些情况下交换
,但在其他情况下不会,在比较方面它们之间的差异是什么?

谢谢...


daniel

解决方案

daniel写道:


我对关键字is感到困惑和==等号,似乎他们可以在某些情况下交换
,但在其他情况下不会,在比较方面它们之间的差异是什么?

谢谢...


daniel



''是''比较对象身份。 ==比较值。


>> a = [1,2,3]
b = [1,2,3]
a是b



False
< blockquote class =post_quotes>


>> a == b



True


在这个例子中,a和b指的是碰巧持有相同值的两个单独的列表。因此,''是''返回False,==返回True。在

另一方面:


>> c = d = [4,5,6]
c是d



True


>> c == d



True


这里,c和d指的是同一个列表。因此,''是''返回true(他们

都指向同一个对象),= =(一个对象总是等于

本身,除非你以一种奇怪的方式重载等式检查。


如果我们试图改变这些列表,很容易看出区别:


>> a.append(4)
a是b



错误


>> a == b



False


>> c.append(7)
c是d



True


>> c == d



True


当我们变异a时,b不受影响。它们是两个不同的列表,并且

改变''a''使得它们不再相等。


当我们改变c时,d IS受到影响;他们引用相同的列表。


如果你曾经说过将''is''应用于
(例如)整数,你可以很容易地迷惑自己。当你可能没想到它时,Python可能会重复使用某些小整数;这样做是为了提高效率。

如果你只比较数字的/值/(用==),那么你将会b / b
从未注意到这一点。 />


>> a = 1
b = 1
c = 1000000
d = 1000000
a是b



True


>> c是d



False


-Kirk McDonald


非常感谢Sybren和Kirk的有用解释。


当我试图检查出来的东西时,发现有趣的是,如果你使用这样的样式来定义变量:

a = b = [ ''a'',''b'']

更改一个列表会影响另一个列表,它们仍然会引用相同的

对象。事实上,似乎所有复合类型(例如字典)

都以这种方式运行。


但是,当列表被替换为其他内置类型时整数



a = b = 3

更改其中一个导致两个对象不同...

最好的问候。


daniel


daniel写道:


当我试图检查出来的东西时,发现有趣的是,如果你用这样的样式来定义变量:

a = b = [''a '',''b'']

更改一个列表会影响另一个列表,并且它们仍然引用相同的

对象。事实上,似乎所有复合类型(例如字典)

都以这种方式运行。


但是,当列表被替换为其他内置类型时整数



a = b = 3

更改其中一个导致两个对象不同...



啊,但在更改变量和更改

对象之间有所区别。


pya = b = [1,2,3]

pya [0] = 6#不要更改变量a,只需更改对象

pya

[6,2,3]

pyb

[6,2,3]

pya = [7, 8,9]#改变变量a;

#它现在是一个不同的对象而不是b

pya

[7,8 ,9 $

pyb

[6,2,3]


对于某些对象,更改对象是不可能的。如果你有
a = b = 3

那么就没有办法将对象3变为4(比如说);

整数对象是不可变的。所以对于这些,要做出改变,

你真的要改变变量,而不是价值。


问候,

Martin


I''m so confused by the keyword "is" and "==" equal sign, it seems they
could be exchanged in some contexts, but not in others, what''s the
difference between them in terms of comparation?

thanks...

daniel

解决方案

daniel wrote:

I''m so confused by the keyword "is" and "==" equal sign, it seems they
could be exchanged in some contexts, but not in others, what''s the
difference between them in terms of comparation?

thanks...

daniel

''is'' compares object identity. == compares values.

>>a = [1, 2, 3]
b = [1, 2, 3]
a is b

False

>>a == b

True

In this example, a and b refer to two separate lists that happen to hold
the same values. Thus, ''is'' returns False, and == returns True. On the
other hand:

>>c = d = [4, 5, 6]
c is d

True

>>c == d

True

Here, c and d refer to the same list. Therefore, ''is'' returns true (they
both refer to the same object), as does == (an object is always equal to
itself, unless you overload the equality check in a weird way).

The distinction can easily be seen if we try to mutate these lists:

>>a.append(4)
a is b

False

>>a == b

False

>>c.append(7)
c is d

True

>>c == d

True

When we mutate a, b is not affected. They are two different lists, and
changing ''a'' makes it so they are no longer equal.

When we mutate c, d IS affected; they refer to the same list.

You can easily confuse yourself if you ever talk about applying ''is'' to
(for example) integers. Python may re-use certain small integers when
you might not expect it to; this is done in the interests of efficiency.
If you only compare the /values/ of numbers (with ==), then you will
never notice this.

>>a = 1
b = 1
c = 1000000
d = 1000000
a is b

True

>>c is d

False

-Kirk McDonald


many thanks to Sybren and Kirk for your helpful explanation.

when I tried to check the stuff out, found sth interesting that if you
define variables in a style like this:
a = b = [''a'', ''b'']
changing one list affects the other, and they still refer to same
object. in fact, seems all compound types (dictionary for instance)
behave in this way.

however, when list is replaced with other built-in types like integers
:
a = b = 3
changing one of them cause the two objects differ...
best regards.

daniel


daniel wrote:

when I tried to check the stuff out, found sth interesting that if you
define variables in a style like this:
a = b = [''a'', ''b'']
changing one list affects the other, and they still refer to same
object. in fact, seems all compound types (dictionary for instance)
behave in this way.

however, when list is replaced with other built-in types like integers
:
a = b = 3
changing one of them cause the two objects differ...

Ah, but make a difference between "change a variable", and "change an
object".

pya = b = [1,2,3]
pya[0] = 6 # don''t change the variable a, just change the object
pya
[6, 2, 3]
pyb
[6, 2, 3]
pya=[7,8,9] # change the variable a;
# it''s now a different object than b
pya
[7, 8, 9]
pyb
[6, 2, 3]

For some objects, "change the object" is impossible. If you have

a = b = 3

then there is no way to change the object 3 to become 4 (say);
integer objects are "immutable". So for these, to make a change,
you really have to change the variable, not the value.

Regards,
Martin


这篇关于什么是关键字“是”对于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆