可以用到 operator.attrgetter()方法来实现
from operator import attrgetter
class User:
def __init__(self, user_id):
self.user_id = user_id
def __repr__(self):
return 'User({})'.format(self.user_id)
def sort_notcompare():
users = [User(23), User(3), User(99)]
print(users)
print(sorted(users, key=lambda u: u.user_id))
print(sorted(users, key=attrgetter('user_id')))
# by_name = sorted(users, key=attrgetter('last_name', 'first_name'))
# min(users, key=attrgetter('user_id'))
if __name__ == "__main__":
sort_notcompare()
使用 operator.attrgetter() 比匿名函数要快