python - 为什么不能在 lambda 表达式中使用 print()?

标签 python lambda functional-programming

为什么是:

p = lambda s: print(s)

语法无效但是:

def do_print(s):
    print(s)
p = lambda s: do_print(s)

有效吗?

最佳答案

lambda 的主体必须是表达式,而不是语句。 print 是一个语句。

更新:如前所述,在 2.x 中,print 是一个语句,而在 Python 3 中,它是一个函数。

关于python - 为什么不能在 lambda 表达式中使用 print()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4380812/

相关文章:

unit-testing - Elixir /ExUnit : passing context from testcase to teardown/cleanup method (on_exit) possible?

javascript - Function.prototype.call.bind(Array.prototype.slice) 与 Array.prototype.slice.call

python - 在 python 中使用 "if all"

c++ - Visual Studio 2012 中的 std::function 目标方法

scala - 为scala中的函数参数设置默认值

scala - 函数式编程,Scala 映射和左折叠

Python/Seaborn : What does the inside horizontal distribution of the data-points means or is it random?

python - 将 Python Selenium WebDriver 与 Firefox GeckoDriver 一起使用时出现无效的安全证书错误

python - 将数据从 (MySQL) 数据库加载到没有模型的 Django 中

lambda - 在Kotlin中生成带有Lambda的列表