[Python-Dev] "is", "==", and sameness

(Greg Weeks) weeks@golden.dtc.hp.com
Fri, 12 May 2000 13:29:52 -0700

>From the Python Reference Manual [emphasis added]:

    Types affect almost all aspects of object behavior. Even the importance
    of object IDENTITY is affected in some sense: for immutable types,
    operations that compute new values may actually return a reference to
    any existing object with the same type and value, while for mutable
    objects this is not allowed.

This seems to be saying that two immutable objects are (in some sense) the
same iff they have the same type and value, while two mutable objects are
the same iff they have the same id().  I heartily agree, and I think that
this notion of sameness is the single most useful variant of the "equals"

Indeed, I think it worthwhile to consider modifying the "is" operator to
compute this notion of sameness.  (This would break only exceedingly
strange user code.)  "is" would then be the natural comparator of
dictionary keys, which could then be any object.

The usefulness of this idea is limited by the absence of user-definable
immutable instances.  It might be nice to be able to declare a class -- eg,
Point -- to be have immutable instances.  This declaration would promise

1.  When the expression Point(3.0,4.0) is evaluated, its reference count
    will be zero.

2.  After Point(3.0,4.0) is evaluated, its attributes will not be changed.

I sent the above thoughts to Guido, who graciously and politely responded
that they struck him as somewhere between bad and poorly presented.  (Which
surprised me.  I would have guessed that the ideas were already in his
head.)  Nevertheless, he mentioned passing them along to you, so I have.