Equality and identity

Donn Cave donn at u.washington.edu
Tue Mar 30 15:00:52 EST 2004


In article <3cfvfkmi337.fsf at nelja.ifi.uio.no>,
 Marius Bernklev <mariube+netnews+ at ifi.uio.no> wrote:

> In python, it seems like immutable objects are equal under both
> equality and identity:
> 
> >>> 5 == 5
> True
> >>> 5 is 5
> True
> >>> "hei" == "hei"
> True
> >>> "hei" is "hei"
> True
> 
> But that isn't true for tuples:
> >>> (5,2) == (5,2)
> True
> >>> (5,2) is (5,2)
> False
> 
> Why not?  Are tuples mutable in some way I've missed, or are there
> other reasons for this behaviour?  To me, it seems natural to concider
> (5,2) and (5,2) the same object, just like "5,2" and "5,2".

Note that the Python interpreter is a program, with finite
limits on its implementation.  It is not an algebraic system.
If you find that (5,2) and (5,2) are not the same object,
rather than trying to infer properties of types from this,
first you might ask `what would it take, as implementor of
Python, to always use the same object for these two quantities?'
and, `how important is it to do so?'  This may lead you to
make a few more experiments with strings and find some holes
in your identity proposition there, too.

Don't make the mistake that people seem recently so prone to
make, of an unhealthy preoccupation with "is" and identity.
When you need to test for equality, always use "==".

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list