An oddity in list comparison and element assignment

Alex Martelli aleax at mac.com
Thu Jun 1 11:15:21 EDT 2006


<michael.f.ellis at gmail.com> wrote:
   ...
> operations.  I think what must be going on is that the 'b' list
> contains replicated references instead of copies of [range(1,3)]*2 .

Right.

> IMO, python's == operator should detect this difference in list
> structure since it leads to different behavior under element
> assignments.

Wrong; equality does not imply any check on identity.  You can consider
the definition of "list A equals list B" as:

-- len(A) == len(B), AND,
-- for each valid index i, A[i] == B[i]

This is an extremely natural definition of "equality" for containers:
"they have EQUAL items" [[in the same order, for containers for which
order is relevant]].  Nowhere in this extremely natural definition does
the IDENTITY of the items come into play.

Therefore, your expectations about the effects of item alterations (for
alterable items) are ill-founded.

Try concisely expressing your "should" -- constructively, as pseudocode
that one could use to check for your "strengthened equality", not in
abstract terms of constraints -- and if (as I strongly suspect) you
cannot find a definition that is as simple, concise and natural as the
two-liner above, this might help convince you that your desired
definition would NOT be the most obvious, natural and fundamental, and
therefore would not be appropriate to pick as part of the language's
core.  Indeed, it's an interesting problem to code up, if one wants any
generality (for example, identity of immutable items _whose items or
attributes are in turn immutable_ probably should not matter even for
your "strengthened" equality... but that's pretty hard to express!-).


Alex
 



More information about the Python-list mailing list