Semantics of ==

Axel Boldt axelboldt at yahoo.com
Thu Mar 18 08:44:28 EST 2004


Erik Max Francis <max at alcyone.com> 
> Axel Boldt wrote:
> 
> > I couldn't find that algorithm in the language reference, only in 5.9:
> > "Tuples and lists are compared lexicographically using comparison of
> > corresponding elements. This means that to compare equal, each element
> > must compare equal and the two sequences must be of the same type and
> > have the same length."
> > 
> > That definition doesn't seem to fully specify equality of list values:
> > ...
> 
> Sure it does; the rule can be used recursively. 

...and then it can run into an infinite loop. I explained that for the
example s==w in the part you deleted. The recursion does not always
have a base case; the rule does not always give a definite truth
value.

> > I hope that the
> > actual result received, i.e. s==w, is not implementation dependent?
> 
> They're also equal in Jython.  It wouldn't surprise me if you could find
> an implementation where they might not compare equal, because this is
> such a pathological case.

... i.e. such a fun case. If there really is an implementation
dependency hidden in something as fundamental as == for lists, that'd
better be mentioned in the language reference.

> > Is there a string
> > function analogous to pickle.dumps or repr which only takes values
> > into account, not internal structure?
> 
> How can you define the value of an arbitrary object without reference to
> its internal structure?

Well, you claim that s and w have the same value yet different
internal structure, so you must work with some definition of "value"
that's different from internal structure. I don't know what it is, and
the language reference doesn't fully specify it.

I.e., I'm interesting in a function val : lists -> strings with the
property val(l1) == val(l2) iff l1 == l2. That would also considerably
clarify the semantics of == for lists, which I still don't understand.

Axel



More information about the Python-list mailing list