real-life identity and "=="
Martin von Loewis
loewis at informatik.hu-berlin.de
Wed May 17 17:35:00 EDT 2000
weeks at golden.dtc.hp.com ((Greg Weeks)) writes:
> In general, mutability affects the notion of identity. A Python operator
> implementing real-life identity would examine the id() of mutable objects,
> but would ignore the id() of immutable objects.
Here, "is" does what you want
>>> a=[1]
>>> b=a
>>> c=[1]
>>> a is b
1
>>> a is c
0
> Because "==" is used in sorting, key comparison, and the "in" operator,
No, that's not true. For all of these, cmp() is used. '==' is defined
in terms of cmp.
== in Python means equality, not identity.
> "==" views class instances as immutable.
No, it doesn't. It checks for equalness. By default, instances are
equal iff they are identical. A custom __cmp__ may change the default.
> Among built-in types, "==" computes real-life identity for all types
> *except* lists and dictionaries.
No, it checks equalness for all of these. For immutable objects, this
happens to coincide with "real-life identity".
> In practice, it doesn't much matter. But it itches. So close!
In the Python community, this is referred-to as "Guido's time
machine", which strikes again.
Martin
More information about the Python-list
mailing list