Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use "return" in lambda function in python?

Tags:

This does not work:

print((lambda :  return None)()) 

But this does:

print((lambda :  None)()) 

Why?

like image 450
ERJAN Avatar asked May 04 '16 09:05

ERJAN


2 Answers

Because return is a statement. Lambdas can only contain expressions.

like image 81
Daniel Roseman Avatar answered Oct 14 '22 04:10

Daniel Roseman


lambda functions automatically return an expression. They cannot contain statements. return None is a statement and therefore cannot work. None is an expression and therefore works.

like image 21
TigerhawkT3 Avatar answered Oct 14 '22 04:10

TigerhawkT3