#-*- coding: utf-8 -*-
class C:
def __init__(self, i):
self.i = i
def __cmp__(self, other):
return cmp(self.i, other.i) #正向返回值
class D:
def __init__(self, i):
self.i = i
def __cmp__(self, other):
return - cmp(self.i, other.i) #这里有 - 则反向返回值
#return cmp(self.i, other.i)
print cmp(C(1), C(2))#第一个数小返回-1
print cmp(C(2), C(2))#相等返回0
print cmp(C(3), C(2))#第一个数小返回1
print "反向返回值"
print cmp(D(1), D(2))
print cmp(D(2), D(2))
print cmp(D(3), D(2))
结果:
-1
0
1
反向返回值
1
0
-1