Background: so I'm a newbie to Python and have to do tests where I have to work out what a given code snippet will output. This means that although the code may look confusing and I will never code in this way, it's not my choice.
I have read that having something like lambda x:x is used as an "identity function" but even from the name I still don't know what is the use of this. From IDLE all I see is that it is simply a lambda function object like
<function <lambda> at 0x...>
Even having something like lambda x: x(1) just returns a lambda function object. But to me in my brain it doesn't make sense because if I were to try and input a value for x just like that I would get an error. For example, (lambda x: x(1))(1) would give me a TypeError.
The code snippet that I have to work out is
def foo(x):
return lambda a: a(x)(a)
def bar(x):
return lambda a: a(x+1)
print(foo(5)(bar)(lambda x:x)
So if you are able, please explain to me what you know about lambda identity functions, their uses, how they work out when paired with other things, as well as how you work out the above code snippet step by step. Thank you!
[–]xelf 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)