+ 1

Lambda function python: print((lambda x: print(x))(5))

print((lambda x: print(x))(5)) Why is this outputting “5” and “None”? Can somebody help? Thankyou.

27th Jul 2023, 3:07 PM
Rajat Kumar
Rajat Kumar - avatar
1 Réponse
+ 9
Rajat Kumar , the inner part of the expression mentioned by you is: (lambda x: print(x))(5) the lambda gets an argument (5), which will be output by the print() function that is inside the lambda. this execution does not have any *explicite* value that is returned. so *none* is used as return value. the mentioned lambda is used as an argument in an other (outer) print() statement, which just outputs the *none value*. remove the outer print() statement to get only the valus of 5. normally lambdas are not used to do outputs, but they *return* values. better to use the outer print() statement and remove the inner ones. sample: print((lambda x: x*2)(5)) this lambda gets 5 as argument, multiplies it by 2. this then used as argument for the print() statement. result is 10.
27th Jul 2023, 3:37 PM
Lothar
Lothar - avatar