1

I'm just starting out as a Python programmer. While doing the Python challenge I learn a lot about the language by looking at other peoples' solutions after I've solved them myself.

I see lambda functions all over the place. They seem easy, but also a bit less readable (at least for me now).

Is there any value in using lambda functions over something else? I'd like to know if it's something worth learning this early in my learning curve!

Tim McNamara
  • 18,019
  • 4
  • 52
  • 83
NoSplitSherlock
  • 605
  • 4
  • 19

4 Answers4

2

You use lambdas where they are needed, and you use list comprehensions, generator expressions etc. when they are more appropriate.

A lambda is a one-expression-function which can be used multiple times, or as a kind of callback. It can produce single values as well as complex ones.

A list comprehension is a thing you evaluate once and then use it as opten as you want. You get a list with the generated values and make use of it.

A generator expression (I mention this as it fits here) is a generator (iterable) which you create and then use exactly once: in a loop, in another generator expression, in a list comprehension or even for returning.

You can even combine this stuff, like this:

f = lambda n: [i for i in range(n, 2*n)]
g = (i * 2 for i in f(10))
l = [i * i for i in g]

ll = lambda n: [i * i * 2 for i in f(n)]
glglgl
  • 89,107
  • 13
  • 149
  • 217
  • Couple of follow-up questions: - What do you mean with a one-expression function? A function that does one thing? - Why use a lamba instead of a regular function? – NoSplitSherlock Feb 10 '13 at 14:01
  • @NoSplitSherlock It is a function which consists of one expression and everything which is allowed in an expression. Especially, not `for`, `while`, `try/except` and other things are possible without further actions. You use lambdas for short things which don't require any further explanation and/or for local use (they are shorter) and you use `def`'d functions for everything else. – glglgl Feb 11 '13 at 16:34
0

Lambdas are used when they're needed, when they make the code more concise but not less clear, and so on. They're not really in competition with list comprehensions (or any other comprehensions). If you find something less readable in Python, chances are you should change it to something you find more readable.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 1
    This is how we'd hope it to be, but as we have varying opinions of when they make code concise but not less clear, so we end up with one guy writing a `map(lambda: blah, filter(lambda: foo, xs))` which others consider worse than `[blah for x in xs if foo]`. –  Feb 10 '13 at 12:29
  • My rule of thumb is: use map when you've got a named function you want to apply to the elements, but if you're mapping or filtering a lambda (or both!) you should be using a list comprehension. Or a dict comprehension, or a set comprehension or a generator comprehension! – babbageclunk Feb 10 '13 at 12:33
  • @wilberforce I fully agree. But not everyone agrees, so this answer is off mark IMHO. –  Feb 10 '13 at 12:34
  • @delnan Does that mean there is no inherent difference between those two line? Does that apply to all cases? Can lambdas always be replaced by another function? – NoSplitSherlock Feb 10 '13 at 14:04
  • @user2039503 `lambda` is just a way to create functions, and precisely equivalent to `def` in that sense. The only difference is that `def` needs a separate statement and a name for the function, while `lambda` is an expression. So yeah, you having `lambda` in the language doesn't allow anything `def` doesn't already allow. –  Feb 10 '13 at 14:06
0

The lambda calculus in its entirety is a very cool thing. You should get used to those concepts as early as possible, as they will make you broader-thinking. To start I suggest reading

http://www.secnetix.de/olli/Python/lambda_functions.hawk

Why are Python lambdas useful?

Lambda function are often used in favor of list comprehensions because they produce less code, are easier to read (if you're once used to it) and are one-liners.

Community
  • 1
  • 1
poitroae
  • 21,129
  • 10
  • 63
  • 81
  • 1
    Your claims are debatable at best. List comprehensions are equally well-suited to one-liners (and that's without getting into whether that's a good thing), and usually just as short. Readability of them is apparently subjective too (and yes, I am most definitely used to them). Also, the ideas of lambda calculus (or more generally and usefully, functional programming) can be applied to Python just fine without using the `lambda` keyword specifically. –  Feb 10 '13 at 12:32
  • @delnan Does that make the usage of Lamba functions more a "style" than a necessity? – NoSplitSherlock Feb 10 '13 at 13:49
  • @NoSplitSherlock Yes, to a large degree, a lot of the samples you see around, they reflect the programmers preference of style. – JCash Feb 10 '13 at 15:54
0

Is there any value in using Lamba functions over something else?

Sometimes you wish to create very short functions but don't really want to name them. I use them a lot for function callbacks for instance.

some_system.add_modifier(lambda x, y, z: return (x*2, y*2, z*2))

Like the other comments suggested, it's not really a matter of choosing either or, but just learning some of the key mechanics of the language. That, of course doesn't mean that one has to use that feature all the time, but it's good to know about it.

I'd like to know if it's something worth learning this early in my learning curve!

I'd say learn about it, but use it where you feel comfortable with it. It's common that programmers go wild with a new feature they've learned. :)

JCash
  • 312
  • 1
  • 3
  • 10