class Box(object):
def __init__(self, ival):
self.value = ival
def __cmp__(self,other):
if self.value < other:
return
elif self.value > other:
return 1
else:return 0
Box(2) < Box(2)
Box(2) <= Box(2)
Box(1) >= Box(2)
Box(3) > Box(2)
Box(0) == Box(1)
Box(0) != Box(0)
Box(1) >= Box(2)
Box(3) > Box(2)
Box(0) == Box(1)
TypeError: an integer is required
You have to compare self.value
to other.value
:
def __cmp__(self,other):
if self.value < other.value:
return -1
elif self.value > other.value:
return 1
else:return 0
otherwise you're comparing an integer to an object