p3被排序了吗
匿名函数中的p1 p2均是实例,并不是参数,p3被排序了吗
2016-12-29
L2 = [L1,Lambda p1,p2:cmp(p1.name,p2.name)]
你确定这是正确的语法?
你确定“匿名函数中的p1 p2均是实例,并不是参数”是一个正确的概念?
class Person(object): pass p1 = Person() p1.name = 'Lisa' p2 = Person() p2.name = 'Adam' p3 = Person() p3.name = 'Bart' L1 = [p1, p2, p3] L2 = sorted(L1, cmp=lambda p1, p2:cmp(p1.name, p2.name)) print L2[0].name print L2[1].name print L2[2].name
>>> class Person(object): ... pass ... >>> p1 = Person() >>> p1.name = 'Lisa' >>> >>> p2 = Person() >>> p2.name = 'Adam' >>> >>> p3 = Person() >>> p3.name = 'Bart' >>> >>> L1 = [p1, p2, p3] >>> L2 = sorted(L1, cmp=lambda p1, p2:cmp(p1.name, p2.name)) >>> >>> print L2[0].name Adam >>> print L2[1].name Bart >>> print L2[2].name Lisa
是的,p3被排序了
举报