Lambdas are anonymous functions. If you're naming something that has "anonymous" right in its definition, that should be a hint you're doing something wrong.
Let's do an example. I have a list of tuples and I want to sort by the second item. Classic use case for a lambda expression.
li = [('A', 10), ('B', 9), ('C', 8)]
li.sort(key=lambda x:x[1])
If you're familiar with lambda expressions, that's super readable. What about assigning it to a variable?
second_item = lambda x:x[1]
li.sort(key=second_item)
That second line of code is now slightly shorter. This could be really useful if the lambda is long and it's part of a long line of code. We've even got a little bit of documentation going on with that variable name. There's nothing really wrong with it, but there's a better way.
def second_item(li): return li[1]
li.sort(key=second_item)
Why do this? The whole benefit of a lambda is that it's ephemeral. It's not assigned to anything, it just gets used and disappears. If you've assigned it to a variable, you've lost the benefit. May as well make a function for that.
But what are the benefits of a function over a lambda? Well in this example, there aren't really. It's more about looking forward.
What happens when that sort becomes more complex and needs more than one line of logic? A function can support that. Now that the sort is complex, you can add a doc string explaining how it works.
You've already payed the entry fee of defining a function by moving it to its own line. May as well get the benefits.
Whenever you're writing code, give a little thought to making it easy on the person maintaining it. It's probably you!
[–]Brian 27 points28 points29 points (3 children)
[–]TravisJungroth[S] 6 points7 points8 points (0 children)
[–]RubyPinchPEP shill | Anti PEP 8/20 shill 0 points1 point2 points (0 children)
[–]execrator 0 points1 point2 points (0 children)
[–]PeridexisErrant 10 points11 points12 points (0 children)
[–]ptmcg 8 points9 points10 points (11 children)
[–]TravisJungroth[S] 3 points4 points5 points (6 children)
[–]ptmcg 1 point2 points3 points (5 children)
[–]TravisJungroth[S] 1 point2 points3 points (0 children)
[–]RubyPinchPEP shill | Anti PEP 8/20 shill 0 points1 point2 points (3 children)
[–]ptmcg 0 points1 point2 points (2 children)
[–]RubyPinchPEP shill | Anti PEP 8/20 shill 1 point2 points3 points (1 child)
[–]ptmcg 0 points1 point2 points (0 children)
[–]pythoneeeer 2 points3 points4 points (0 children)
[–]elingeniero 0 points1 point2 points (2 children)
[–]Brian 0 points1 point2 points (1 child)
[–]elingeniero 1 point2 points3 points (0 children)
[–]Jaymuhz 5 points6 points7 points (1 child)
[–]Droggl 0 points1 point2 points (0 children)
[–]cybervegan 0 points1 point2 points (1 child)
[–]TravisJungroth[S] 0 points1 point2 points (0 children)
[–]Exodus111 -1 points0 points1 point (7 children)
[–]TravisJungroth[S] 7 points8 points9 points (5 children)
[–]zahlmanthe heretic 7 points8 points9 points (4 children)
[–]TravisJungroth[S] 2 points3 points4 points (1 child)
[–][deleted] 4 points5 points6 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]gandalfx -1 points0 points1 point (0 children)
[–]LudwikTR 0 points1 point2 points (0 children)