1

I'm pretty new to Python (and programming in general). I was wondering, since lambda and functions are very similar, when is it proper to use which and what are the differences between them?

The reason I'm asking is I've only seen lambda used for very basic and simple programs, such as:

sq = lambda x: x**2 
print(sq(25))

While functions can be much more complicated like having multiple parameters, different looping types, if/else, recursive, calling another function (composition, I think), etc.

I know you can call a function inside a lambda like:

def turnUppercase(n):
    return n.upper()

a = lambda x: turnUppercase(x)
print(a('Cookie'))

That example is pointless, but still... I've never tested the limits of lambda by trying other things.

What are the limits of lambda? How can you extend the functionalities of lambdas (if only to impress people) to match that of functions? (Calling a function inside lambda, calling another lambda, loops inside, and so on).

Note I'm asking about Python 3.

Thanks!

4
  • Do you plan on assigning it to a name? Use a function (def). Otherwise use lambda Commented Jun 30, 2015 at 2:44
  • You can assign a lambda to a name too... Commented Jun 30, 2015 at 2:47
  • @Navith I don't think name assignment is really the defining thing as you can do my_fun = lambda x: x + 1 for example and that's the same as def my_fun(x): return x +1. Really it's just a matter of convenience and readability though lambdas are limited in what you can express: docs.python.org/3.1/reference/expressions.html#lambda. In particular they can't contain statements Commented Jun 30, 2015 at 2:47
  • You can assign a name to it, but if you're going to then what you really want is to make a function with def. Commented Jun 30, 2015 at 3:38

1 Answer 1

2

A lambda is a nameless function. In python, it has to fit in one line. It's mostly useful only when you're calling a function that takes another function as an argument.

Example:

listOfLists.sort(key=lambda x:x[1]) #Sort list of lists by second element

(see sorting: Key Functions. Probably the most common valid use of lambdas)

You can do a lot of stupid stuff in lambdas (check out any code golf written in python) but it's generally recommended to keep them simple if you're going to use them in Actually Maintained Code.

While functions can be much more complicated like having multiple parameters, different looping types, if/else, recursive, calling another function (composition, I think), etc.

Incidentally, I think the only one of these you can't do in a lambda is recursion.

Multiple parameters: lambda x,y: x**2+y**2

Loops (technically): lambda x: [subprocess.call('pip install --upgrade ' + dist.project_name, shell=True) for dist in pip.get_installed_distributions()] (and yes I know I'm a horrible person)

If/else: lambda x: "Blue" if x > 1000 else "Orange"

And as for what you can't do in lambdas...uh, keyword arguments? *args? Any bit of complexity without your code looking like a drunk cat wandered by and hit parens, square brackets and curly brackets randomly?

I think the general rule of "If your boss came up to you and asked the question 'Why is this a lambda' and you can answer immediately AND explain what the lambda does, you might be justified in using a lambda. Otherwise, it's better to err on the side of not using one."

Sign up to request clarification or add additional context in comments.

2 Comments

operator.itemgetter for your first example
In short functions are more often used than lambdas and unless it's really simple one-line codes, use functions instead of lambdas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.