addressof object with id()

Roy Smith roy at panix.com
Sat Mar 23 21:20:05 EDT 2013


In article <mailman.3659.1364086613.2939.python-list at python.org>,
 Chris Angelico <rosuav at gmail.com> wrote:

> On Sun, Mar 24, 2013 at 11:49 AM, Dave Angel <davea at davea.name> wrote:
> > You can assume that if the id's are equal, the objects are equal.  But you
> > can't assume the inverse or the converse.
> 
> To be more specific: If the ids are equal, the objects are identical.
> Doesn't mean they'll compare equal - for instance, float("nan") isn't
> equal to itself. But for most situations, you can assume that
> identical objects compare equal.


>>> n = float("nan")

# No real surprise here
>>> n == n
False

# But this is kind of weird
>>> [n] == [n]
True

In fact, that's actually a bug (or at least, contrary to the documented 
behavior).  The docs (http://docs.python.org/2/library/stdtypes.html) 
say:

> In particular, tuples and lists are compared lexicographically by comparing 
> corresponding elements. This means that to compare equal, every element must 
> compare equal and the two sequences must be of the same type and have the 
> same length

and that's not what's happening here:

>>> l1 = [n]
>>> l2 = [n]
>>> l1 == l2
True
>>> l1[0] == l2[0]
False



More information about the Python-list mailing list