5 ms·
Why Python's Lambda is Broken and Can't be Fixed
- jamesseattle 14y agoI have never used lambda in years of programming Python. If I need a one time function I define a function and use it once. To me it is more readable and readability is one of Python's greatest virtues.
- dragonwriter 14y agoThey aren't broken, they work exactly as intended. They are more limited than similar constructs in some other languages, which compels using named functions in situations where in other languages one might use anonymous ones. On balance, this isn't really a big deal. Also, its quite possible to support multistatement lambdas within Python's indentation-sensitive structure; the decision not to do so isn't a result of technical limitations but of aesthetic judgements.
- lazzlazzlazz 14y agoForgive the amateur question: when would one absolutely need to have a function defined inline?
- csense 14y agoIt's not a matter of absolute need; it's a matter of shortening functions. For example, say I have a list of username and number of posts: users = [["user1", 500], ["n00b", 15], ["thebestuser", 177]] I want to sort the users according to number of posts. The sorted() function takes a parameter key=, you can do it this way without lambda: def get_users_by_posts(): def get_post_count(user): return user[1] return sorted(users, key=get_post_count) The lambda implementation is a one-liner: def get_users_by_posts(): return sorted(users, key=lambda u : u[1]) Well, technically a twoliner, unless you use lambda more aggressively than most people: get_users_by_posts = lambda : sorted(users, key=lambda u : u[1]) In general, you can always expand your lambda's into def's, but it can make your code longer and pollute the code with named functions that do trivial things.
- _ZeD_ 14y agowell, in this particular case, operator.itemgetter is your friend :D import operator def get_users_by_posts(): return sorted(users, key=operator.itemgetter(1)) then, if you really want to, you can use functools.partial to fill the parameters from functools import partial from operator import itemgetter get_users_by_posts = partial(sorted, users, key=itemgetter(1))
- anthonyb 14y agoNo, 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)
- csense 14y agoJavascript's syntax for anonymous functions is awesome. Coffeescript is okay, but it's just a little too terse -- it can sometimes be difficult to figure out what's going on. The lambda syntax is indeed poor; since it forces the function body has to be an expression, it means you have to jump through hoops to get your implementation to be an expression (or give up and define + use a function).
- pcote 14y agoThe upshot of Python lambdas is they're really convenient in simple situation. It causes trouble when debugging though. Sometimes, I need to stick breakpoints in places that force me to rewrite a lambda as a regular function. The more that happens, the less I like lambdas.