how to compare two objects in class using __cmp__ method in Python?
<pre><code>class Box(object):
def __init__(self, ival):
    self.value = ival
def __cmp__(self,other):
    if self.value &lt; other:
        return 
    elif self.value &gt; other:
        return 1
    else:return 0
</code></pre>

<p>But when I wanna test the program with :</p>

<pre><code>Box(2) &lt; Box(2)
Box(2) &lt;= Box(2)
Box(1) &gt;= Box(2)
Box(3) &gt; Box(2)
Box(0) == Box(1)
Box(0) != Box(0)
</code></pre>

<p>Part of the code below:</p>

<pre><code>Box(1) &gt;= Box(2)
Box(3) &gt; Box(2)
Box(0) == Box(1)
</code></pre>

<p>has shown </p>

<blockquote>
  <p>TypeError: an integer is required</p>
</blockquote>

<p>Anyone knows why?</p>

<p>EDITED :I added the -1 before but somehow I deleted it -.-
sorry for asking such careless question!</p>

<p>Note that the <code>__cmp__</code> method id deprecated now - you should really use the so-called "rich comparison" methods - <code>__eq__</code> and friends.</p>

<p>The error is being caused because you are attempting to compare an integer with a <code>Box</code> object, and no such comparison is defined. Perhaps what you need is more like</p>

<pre><code>        if self.value &lt; other.value:
            return -1
        elif self.value &gt; other.value:
            return 1
        else:
            return 0
</code></pre>

<p>but then you might need to take into account whether <code>other</code> is a <code>Box</code> or not. If it's not, what does the comparison mean?</p>

python python-2.7