class Student(object):
def __init__(self,name,score):
self.name=name
self.score=score
def __str__(self):
return '(%s,%s)'%s(self.name,self.score)
__repr__=__str__
def __cmp__(self,s):
if self.name<s.name:
return -1
elif self.name>s.name:
return 1
else :
return 0
>>> L=[Student('Tim',99),Student('Bob',88),Student('Tom',77)]
>>> print sorted(L)
[<__main__.Student object at 0x02B23290>, <__main__.Student object at 0x02B23370>, <__main__.Student object at 0x02B233B0>]
代码是这样的,在IDE中结果上,为甚?
相关分类