title: __lt__ instead of __cmp__
body: <p>Python 2.x has two ways to overload comparison operators, <a href="http://docs.python.org/2.6/reference/datamodel.html#object.__cmp__" rel="noreferrer"><code>__cmp__</code></a> or the "rich comparison operators" such as <a href="http://docs.python.org/2.6/reference/datamodel.html#object.__lt__" rel="noreferrer"><code>__lt__</code></a>.  <strong>The rich comparison overloads are said to be preferred, but why is this so?</strong></p>
  
  <p>Rich comparison operators are simpler to implement each, but you must implement several of them with nearly identical logic.  However, if you can use the builtin <code>cmp</code> and tuple ordering, then <code>__cmp__</code> gets quite simple and fulfills all the comparisons:</p>
  
  <pre><code>class A(object):
    def __init__(self, name, age, other):
      self.name = name
      self.age = age
      self.other = other
    def __cmp__(self, other):
      assert isinstance(other, A) # assumption for this example
      return cmp((self.name, self.age, self.other),
                 (other.name, other.age, other.other))
  </code></pre>
  
  <p>This simplicity seems to meet my needs much better than overloading all 6(!) of the rich comparisons.  (However, you can get it down to "just" 4 if you rely on the "swapped argument"/reflected behavior, but that results in a net increase of complication, in my humble opinion.)</p>
  
  <p><strong>Are there any unforeseen pitfalls I need to be made aware of if I only overload <code>__cmp__</code>?</strong></p>
  
  <p>I understand the <code>&lt;</code>, <code>&lt;=</code>, <code>==</code>, etc. operators can be overloaded for other purposes, and can return any object they like.  I am not asking about the merits of that approach, but only about differences when using these operators for comparisons in the same sense that they mean for numbers.</p>
  
  <p><em>Update:</em> As Christopher <a href="https://stackoverflow.com/questions/1061283/style-why-lt-instead-of-cmp/1061323#1061323">pointed out</a>, <code>cmp</code> is disappearing in 3.x. <strong>Are there any alternatives that make implementing comparisons as easy as the above <code>__cmp__</code>?</strong></p>