On 10/29/07, Steven Bethard <[EMAIL PROTECTED]> wrote: > On 10/29/07, David A. Wheeler <[EMAIL PROTECTED]> wrote: > > I think several postings have explained better than I have on why __cmp__ > > is still very valuable. (See below.) > > > > Guido van Rossum posted earlier that he was willing to entertain a PEP to > > restore __cmp__, so I've attempted to create a draft PEP, posted here: > > http://www.dwheeler.com/misc/pep-cmp.txt > > Please let me know if it makes sense. Thanks. > > I think the PEP's a little misleading in that it makes it sound like > defining __lt__, __gt__, etc. is inefficient. I think you want to be > explicit about where __lt__, __gt__ are efficient, and where __cmp__ > is efficient. For example:: > > * __lt__ is more efficient for sorting (given the current implementation) > * __cmp__ is more efficient for comparing sequences like tuples, where > you always need to check for equality first, and you don't want to > have to do an == check followed by a < check if you can do them both > at the same time. (This is basically the same argument as for Decimal > -- why do two comparisons when you can do one?)
When implementing a large, totally ordered object (>=2 fields), both __lt__ and __cmp__ should probably be implemented by calling __cmp__ on the fields. If you decide to implement __lt__ by letting it forward to __cmp__, the cutoff might be at 3 fields. Partial orders (what the PEP calls "asymmetric classes") cannot, of course, be implemented with __cmp__ and should have it return NotImplemented. Well, if we wanted to diverge from most other languages, we could extend __cmp__ to let it return a distinguished "Unordered" value, which returns false on all comparisons with 0. This is similar to Fortress's approach, which returns one of 4 values from a PartialOrder's CMP operator: EqualTo, LessThan, GreaterThan, and Unordered. Haskell has only a total ordering class in the core libraries, while Scala has a PartiallyOrdered trait that returns None from its compare method for unordered values. For Python, I think I favor reviving __cmp__ for totally ordered types, and asking that partially ordered ones return NotImplemented from it explicitly. Jeffrey _______________________________________________ Python-3000 mailing list Python-3000@python.org http://mail.python.org/mailman/listinfo/python-3000 Unsubscribe: http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com