Lambda functions are awesome. They allow you to define higher-order functions inline. The general format is lambda args: expression. In this case, x is the argument passed into the lambda function. Because make_adder returns a lambda function, whatever you pass into make_adder is set as n. So when you pass in make_adder(2) you get a lambda function that adds 2 to the argument (x).
Decomposing your original snippet:
def make_adder(n):
return lambda x: x + n
plus_2 = make_adder(2) # returns lambda x: x + 2
plus_2(5) # executes lambda expression, x + 2 with x=5
Starting from scratch:
5 + 2 # 7
plus_two_fn = lambda x: x + 2 # Add 2 when you call plus_two_fn(x)
plus_two_fn(3) # returns 5 (3 + 2)
def plus_num_fn(num):
return lambda x: x + n # Allow you to define plus "any" number
plus_one_fn = plus_num_fn(1) # lambda x: x + 1
plus_one_fn(2) # returns 3 (2 + 1)
def foo(x): <do something with x>