Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
生成python添加上一个函数结果_Python_Yield - Fatal编程技术网

生成python添加上一个函数结果

生成python添加上一个函数结果,python,yield,Python,Yield,我想编写一个使用yield的函数,但每次发送给函数的字段值都是以前的结果: 例如,如果调用 f(5)返回10,下一个调用将是f(10)。如果f(10)的结果返回18,则下一个调用将是f(18) 我怎么做? 我写了这段代码: def my_function(f,init_value): yield init_value while True: yield f(init_value) 但它总是返回对f(init_值)的调用,而不是像我期望的那样。每次保存该值 def my_fun

我想编写一个使用yield的函数,但每次发送给函数的字段值都是以前的结果:

例如,如果调用

f(5)返回10,下一个调用将是f(10)。如果f(10)的结果返回18,则下一个调用将是f(18)

我怎么做? 我写了这段代码:

def my_function(f,init_value):
  yield init_value
  while True:
     yield f(init_value)

但它总是返回对f(init_值)的调用,而不是像我期望的那样。

每次保存该值

def my_function(f, value):
  yield value
  while True:
    value = f(value)
    yield value