b = lambda x: (lambda x: x)
print(b(88))
Why does the python code prints function instead of 88? Why is this different from the code below?
b = lambda x:c(x)
c = lambda x:x
print(b(88))
b = lambda x: (lambda x: x)
Your code is equivalent to this:
def b(x): # <- This "x" is not used
def anonymous(x):
return x;
return anonymous
So you can call it like this:
>>> b("anything")(88)
88
This code:
c = lambda x:x
b = lambda x:c(x)
is equivalent to:
def c(x):
return x
def b(x):
return c(x)
So if you call b(88), it will call c(88) and return the result.
c(88) just returns 88.
Functions are first class objects in python. This means that they can be treated as values. In your case, your b function is returning a function (let's call it c). Note that I got rid of the x parameter in your b function, because it's useless.
b = lambda: (lambda x: x)
c = b()
Now you can call c, since it's a function (c needs a parameter, x):
print(c(88)) # this prints 88
Thank you guys for the help. I think I am getting a hang of it:
b = lambda x: (lambda x: x)
is equivalent to:
def lambda_1(x):
def lambda_2(x):
return x
return lambda_2
b = lambda_1
whereas
b = lambda x: (lambda x: x)(x)
is equivalent to:
def lambda_1(x):
def lambda_2(x):
return x
return lambda_2(x)
b = lambda_1
b(88), a function. Not sure why you expected to see88.b(1234567)(88)(as you may notice, the first parameter is useless, because you are not using it)b=lambda x: (lambda x: x)(x); print(b(88));)def identity(x)return x. Solambda x: xis equivalent toidentity. In order to call this function, I have to provide the argument,identity(88). Here (lambda x: x)(x) is equivalent to identity(x) rather than identity. So I am calling this function within b, whereas in the previous example I only defined within b not called? Am I understanding it right?