0
Question related to lambda
x='cyzxyz' (lambda x : print(x))(x) O.p - cyzxyz Why there an x outside of print statement ? Could anyone explain the second line pls
3 ответов
+ 3
x = 'cyzxyz'
(lambda x : print(x))(x)
^
parameter of the function
(lambda x : print(x))(x)
^
function "body", x refers to the parameter
(lambda x : print(x))(x)
^
argument that you pass to the function; x refers to the variable x, that holds the string
You could rewrite the code like this:
y = 'somestring'
(lambda x : print(x))(y)
or
def fun(x):
print(x)
fun(y)
+ 3
Again we can rewrite the code to make it clearer:
a=[lambda x=num:x*10 for num in range(10)]
for fun in a:
print(fun())
Here num is the default value for the x parameter
0
Lisa Thanks u so much for ur explanation. And I have one more query
a=[lambda x=x:x*10 for x in range(10)]
for i in a:
print(i())
What does lambda x=x mean ? Could u explain in simple terms pls