开发者

Why can't print() be used in a lambda expression?

开发者 https://www.devze.com 2023-01-29 03:08 出处:网络
Why is: p = lambda s: print(s) invalid syntax but: def do_print(s): print(s) p = lambda s: do_开发者_如何学编程print(s)

Why is:

p = lambda s: print(s)

invalid syntax but:

def do_print(s):
    print(s)
p = lambda s: do_开发者_如何学编程print(s)

valid?


The body of a lambda has to be an expression, not a statement. print is a statement.

Update: As pointed out, in 2.x, print is a statement while in Python 3, it is a function.


which version of python are you using?; in python 2.7 (and before), print is a statement while in python 3 it's a function


its the way the language is read it can't do p = lambda s: print(s) all in one step

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号