frozenjim
frozenjim

Reputation: 796

How does a lambda function get it's value?

Reviewing the W3Schools Python tutorial, I am puzzled by their example of lambda.

Where on earth is the value for variable "a" coming from? Sure, "a" is the lambda - but where is it getting a value from?

def myfunc(n):
  return lambda a : a * n

mytripler = myfunc(3)

print(mytripler(11))

If I call print(myfunc(3)) on it's own, I get an error. The function only works if the function is CALLED by another function. How does lambda a know any of this?

I fear I am missing something very fundamental about lambda functions.

Upvotes: 1

Views: 2548

Answers (3)

Steven
Steven

Reputation: 2133

a in your example is assigned the value 11.

Think of the definition of a lambda function as "when given an input, return this". You can name the input anything, so let's call it x. lambda x : x + 1 means "given an input that we'll call x : return x + 1".

You have an extra layer of a function returning a lambda, but if we break it down, the purpose of myfunc is to dynamically generate a lambda function. When you call myfunc(3), what you now have in your hands is a lambda function that looks like this: lambda a : a * 3. This is what the variable mytripler contains. If you print(mytripler), you see something like this: <function myfunc.<locals>.<lambda> at 0x...>, which tells you that it's some lambda function. mytripler contains a function, not a value.

What does this lambda function do? It says "given one input parameter which we'll call a : return a * 3".

What do you do with functions? You call them! And send any parameters along with the call. How many parameters does our lambda function take? However many variables precede the colon in the lambda function definition, in this case one: a. And so we call the lambda stored in mytripler by sending it one parameter: mytripler(11). 11 is the input to the lambda function, which is first assigned to a and returns a * 3 -> 33.

You could have a lambda that takes 2 inputs that we'll call x and y: adder = lambda x, y : x + y What does this lambda do? It says "given 2 inputs that we'll call x and y : return their sum". How do you call it? You have to call adder with two parameters: adder(2,3) returns 5. 2 is assigned to x, 3 is assigned to y and the function returns their sum.

What if we call adder(1)? TypeError: <lambda>() missing 1 required positional argument: 'y'

From this you can see that Python knows how many input parameters the lambda function expects from its definition and cannot work without first getting all the parameters it needs.

Upvotes: 1

Sheldore
Sheldore

Reputation: 39052

What myfunc(3) does is that it assigns 3 to n and then this value is used in the lambda expression. So the expression becomes 3*a. Your lambda function has an independent variable a. When you return lambda a : a * n your mytripler is now acting as an object (function) whose argument is a and the action is to compute a*3. So finally when you call

mytripler(11)

you are assigning 11 to your independent variable a thereby getting 33

Upvotes: 3

g.d.d.c
g.d.d.c

Reputation: 47988

Lambdas are just shorthands for full declarations. That makes your example functionally identical to this:

def myfunc(n):
  def inner(a):
    return a * n
  return inner

The lambda is a function, and when you call it, you provide it an argument (a in this example).

Upvotes: 6

Related Questions