Python 在哪里以及如何存储产生的数据?
Where and how Python stores yielded data?
假设我有一个如下所示的函数:
def gen_fn():
yield from range(10000)
然后我将它分配给一百万个变量:
d = {}
for x in range(100000):
d[f'variable{x}'] = fn()
现在每个变量 variable0
到 variable999999
指向不同的生成器:
d
# 'variable0': <generator object fn at 0x155e40390>,
# 'variable1': <generator object fn at 0x155e405e8>,
# 'variable2': <generator object fn at 0x155e40570>,
# 'variable3': <generator object fn at 0x155e404f8>,
# 'variable4': <generator object fn at 0x155dfe840>,
...
我理解它将每个 variable
分配给一个单独的生成器,因为它们背后的想法,我可能想要获得 next(d['variable0'])
并且不影响 next(d['variable1'])
,是这样吗?
现在假设我 运行 next(d['variablex']
其中 x 是 range(1000000)
随机数 next()
次的唯一数字,现在每个 generator
来自 gen_fn()
产生不同的值。我感兴趣的是 Python 在哪里跟踪每一百万台发电机当前产生的产量?
赋值 d[f'variable{x}'] = gen_fn()
为每个字典键分配一个指向生成器函数的指针。
稍后调用这些函数是独立的:
例如
# 0
print(next(d['variable0']))
# 0
print(next(d['variable1']))
# 1
print(next(d['variable1']))
生成器根据 yield
文档维护状态:
When a generator function is called, it returns an iterator known as a generator. That generator then controls the execution of the generator function. The execution starts when one of the generator’s methods is called. At that time, the execution proceeds to the first yield expression, where it is suspended again, returning the value of expression_list to the generator’s caller.
By suspended, we mean that all local state is retained, including the current bindings of local variables, the instruction pointer, the internal evaluation stack, and the state of any exception handling. When the execution is resumed by calling one of the generator’s methods, the function can proceed exactly as if the yield expression were just another external call. The value of the yield expression after resuming depends on the method which resumed the execution.
假设我有一个如下所示的函数:
def gen_fn():
yield from range(10000)
然后我将它分配给一百万个变量:
d = {}
for x in range(100000):
d[f'variable{x}'] = fn()
现在每个变量 variable0
到 variable999999
指向不同的生成器:
d
# 'variable0': <generator object fn at 0x155e40390>,
# 'variable1': <generator object fn at 0x155e405e8>,
# 'variable2': <generator object fn at 0x155e40570>,
# 'variable3': <generator object fn at 0x155e404f8>,
# 'variable4': <generator object fn at 0x155dfe840>,
...
我理解它将每个 variable
分配给一个单独的生成器,因为它们背后的想法,我可能想要获得 next(d['variable0'])
并且不影响 next(d['variable1'])
,是这样吗?
现在假设我 运行 next(d['variablex']
其中 x 是 range(1000000)
随机数 next()
次的唯一数字,现在每个 generator
来自 gen_fn()
产生不同的值。我感兴趣的是 Python 在哪里跟踪每一百万台发电机当前产生的产量?
赋值 d[f'variable{x}'] = gen_fn()
为每个字典键分配一个指向生成器函数的指针。
稍后调用这些函数是独立的:
例如
# 0
print(next(d['variable0']))
# 0
print(next(d['variable1']))
# 1
print(next(d['variable1']))
生成器根据 yield
文档维护状态:
When a generator function is called, it returns an iterator known as a generator. That generator then controls the execution of the generator function. The execution starts when one of the generator’s methods is called. At that time, the execution proceeds to the first yield expression, where it is suspended again, returning the value of expression_list to the generator’s caller.
By suspended, we mean that all local state is retained, including the current bindings of local variables, the instruction pointer, the internal evaluation stack, and the state of any exception handling. When the execution is resumed by calling one of the generator’s methods, the function can proceed exactly as if the yield expression were just another external call. The value of the yield expression after resuming depends on the method which resumed the execution.