且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

调用包含yield的函数时会发生什么?

更新时间:2022-04-28 23:57:19

正确。调用 double_inputs 不会执行任何代码。它只是返回一个 generator 对象。 def 语句被解析时发现的体内 yield 表达式的存在,更改 def 语句的语义以创建 generator 对象而不是 function 对象。

Correct. Calling double_inputs never executes any of the code; it simply returns a generator object. The presence of the yield expression in the body, discovered when the def statement is parsed, changes the semantics of the def statement to create a generator object rather than a function object.