对用于处理 Python 生成器中的参数的语法感到困惑
Confused by the syntax used to process the argument in a Python Generator
在函数 cache_gen 中,参数被分配给 values 为:values = source(),而不是 values = source?
我意识到他们必须这样做以确保参数成为生成器,但我一直无法找到任何文档来解释这种语法。
我只是想确保我理解正确。
import numpy as np
SAMPLER_CACHE = 10000
def cache_gen(source):
values = source()
while True:
for value in values:
yield value
values = source()
if __name__ == "__main__":
randn_gen = cache_gen(lambda: np.random.standard_normal(SAMPLER_CACHE))
他们将 lambda 作为参数传递。他们调用 source()
来获取 lambda 的输出,它本身就是 np.random.standard_normal(SAMPLER_CACHE)
的输出。然后他们使用生成器一次输出所有值。
在函数 cache_gen 中,参数被分配给 values 为:values = source(),而不是 values = source?
我意识到他们必须这样做以确保参数成为生成器,但我一直无法找到任何文档来解释这种语法。
我只是想确保我理解正确。
import numpy as np
SAMPLER_CACHE = 10000
def cache_gen(source):
values = source()
while True:
for value in values:
yield value
values = source()
if __name__ == "__main__":
randn_gen = cache_gen(lambda: np.random.standard_normal(SAMPLER_CACHE))
他们将 lambda 作为参数传递。他们调用 source()
来获取 lambda 的输出,它本身就是 np.random.standard_normal(SAMPLER_CACHE)
的输出。然后他们使用生成器一次输出所有值。