10

The other day I wrote a lambda akin to the following:

fetch = lambda x: myDictionaryVariable.get(x, "")

But now I just learned that to a point you can separate python statements with a ; instead of a newline and then learned that you can do simple statements on 1 line even with the colon. So I realized I could also write this:

def fetch(x): return myDictionaryVariable.get(x, "")

Not that I'm using the ; here, but if I needed to I could, and thusly provide even more functionality for my 1-line function. I could write:

def strangeFetch(x): y = "unicorn"; return menu.get(x, y)

So why do I need lambdas at all? Why are they even a part of python? In view of this, what do they add?

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
  • 10
    Don't sweat it - *every* time someone asks about lambda, close votes soon follow. I suspect they're Lisp'ers who fear reading bad things about lambda ;-) BTW, don't get enchanted with 1-liner disease: code is read more often than it's written, and `strangeFetch()` is atrocious. – Tim Peters Dec 20 '13 at 03:26
  • 2
    It also happens any time someone asks "Why is language X designed like this?" – Barmar Dec 20 '13 at 03:27
  • I don't know if I would have voted to close on opinion grounds; I might have voted to close as a dup. Look at the "related" questions on the right. – DSM Dec 20 '13 at 03:27
  • Barmar I did read that but I thought my question was different in that was I specifically asking when to use them vs. using 1-line def statements. Oh hey @TimPeters I was also wondering if you had any official insight on best practices for this question I just asked: http://stackoverflow.com/questions/20695516/how-to-indicate-that-a-function-expects-a-function-as-a-parameter-or-returns-a – temporary_user_name Dec 20 '13 at 03:30
  • @Barmar, I actually got one of those re-opened once! It's amazing how often there are *reasons* for design decisions. Of course it's also amazing how often there aren't ;-) – Tim Peters Dec 20 '13 at 03:31
  • 2
    It's not that there isn't a reason for design decisions, it's that discussion of those reasons isn't considered on-topic for SO. – Barmar Dec 20 '13 at 03:31
  • Aerovistae, no, I've never used function annotations in Python. If I ever find myself tempted to use them, I'd probably pick a different language instead ;-) – Tim Peters Dec 20 '13 at 03:32

3 Answers3

10
  1. Lambda functions don't need a name. They don't clog your namespace just for a function which is used only once.

    a = [[1], [1, 2], [1, 2, 3]]
    print min(a, key = lambda x:len(x))
    print locals()
    

    As you can see, though we created a lambda function (in this case, we could have directly used len), it does not add up to the local namespace.

  2. They are use and throw type functions. They could be GCed after the line in which they are used, unless they are assigned to some variables.

  3. They don't allow any python statements and assignments, so they can be little trusted with side-effects.

    l = [1, 2]
    lambda: l = []
    

    This will throw an error, SyntaxError: can't assign to lambda. (You can mutate mutable objects though).

  4. Sometimes, they can be used to beat the effect of closures.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
6

lambda is useful when you want to use an anonymous function as a parameter to another function. If it's only being used in that one place, there's no need to assign a name to it.

The ability to assign a lambda to a variable just falls out of the orthogonality of the language: a variable can hold any type of value, and a function created by lambda is just a value.

Barmar
  • 741,623
  • 53
  • 500
  • 612
4

A lambda expression is itself a value, in a way that a def statement isn't. This is because a def statement is essentially a function literal + an assignment, and in Python an assignment isn't an expression.

But you don't need lambdas. Everything you could do with one you could do with a named function.

babbageclunk
  • 8,523
  • 1
  • 33
  • 37