NoSplitSherlock
NoSplitSherlock

Reputation: 615

Is there any reason to use a lambda function over e.g a list comprehension?

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!

Upvotes: 1

Views: 207

Answers (4)

JCash
JCash

Reputation: 322

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. :)

Upvotes: 0

glglgl
glglgl

Reputation: 91017

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)]

Upvotes: 2

poitroae
poitroae

Reputation: 21357

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.

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249093

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.

Upvotes: 0

Related Questions