Lambdas in practice
Use small functions with more confidence.
Give another function your rule.
A function can be passed as a value. Here, apply_twice takes a rule and a starting value, then calls that rule twice.
See how apply_twice works
def apply_twice(func, value):
return func(func(value))The inner func(value) runs first. Its result becomes the input to the outer call. A function that accepts or returns a function is called a higher-order function.
lambda x: x + 3 creates the function to pass.(lambda x: x + 3)(5) calls it immediately and produces 8.
The caller decides how to use the result.
Each of these functions calls a lambda for each item. What it does with the returned value is different.
words = ["pear", "fig", "banana"]
list(map(lambda word: len(word), words))
"pear"4a new value"fig"3a new value"banana"6a new value[4, 3, 6]map uses the returned lengths as new items. len(word) counts the letters in a word.
When do these lambdas run?
map and filter return iterators: values are produced as you request them. In these examples, list(...) requests all the results, so the lambdas run then.
sorted calculates its keys and returns a new list during the call. The keys decide the order; the original items go into the result.
Review the basics: Meet map() or Introduction to filters.
More inputs. An optional default.
Separate parameters with commas. A default supplies a value when you leave that argument out.
Passing 0 still supplies an argument. The default is used only when you omit y. Leaving out the required x causes a TypeError.
Can I use keyword arguments?
Yes. add(x=2, y=3) returns 5. Lambdas use the same argument rules as functions defined with def.
One expression can make a choice.
A conditional expression has two possible values. Python checks the condition and evaluates only the chosen branch.
% gives the remainder after division. A remainder of 0 means the number is even. The body is one expression: value_if_true if condition else value_if_false.
What can a lambda return?
Any value, including a number, string, Boolean, or list. For example, lambda x: [x, x * 2] returns [3, 6] when called with 3.
Returning and printing are different. lambda x: print(x) displays the input but returns None, because that is what print returns.
When a value is read matters.
The body runs when the function is called. A default is evaluated earlier, when the function is created.
lambda: has no parameters. remember() leaves out its optional argument, so it uses the saved default. This timing also applies to def.
Is a default always a snapshot?
No. A default stores an object reference. In this example, rebinding n to a new number leaves the old default in place. If the default were a list, changing that same list would still be visible through the default.
What does the second call return?
1 of 6Take your time. You can try each answer again.
You can put lambdas to work.
Pass a function where a rule is needed.
Know how its result is used and when its values are read.
lambda x: x * 2
Review the lesson