7 ms·
No, don't do this. The people maintaining your code will hate you if everything is an unnamed lambda. A better way to do it is to take your core data structure
by anthonyb 14y ago
No, don't do this. The people maintaining your code will hate you if everything is an unnamed lambda.
A better way to do it is to take your core data structure, in this case a user, and turn it into a class, dictionary or named tuple. Classes are the best way - there are all sorts of ways to put a nice interface on them.
For example:
class User(object):
def __init__(self, name, post_count):
self.name = name
self.post_count = post_count
def __repr__(self):
return '<User name="%s", post_count="%s">' % (self.name, self.post_count)
def __cmp__(self, other_user):
# larger is better
return -cmp(self.post_count, other_user.post_count)
# set up data
users_ = [["user1", 500], ["n00b", 15], ["thebestuser", 177]]
users = []
for user in users_:
users.append(User(*user))
print users
# sort users by post count by default
print sorted(users)