daniel said:
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:
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.
False
-Kirk McDonald