这个问题参考了这篇关于关键字“yield”的[Stackoverflow][1]帖子。
我的理解是,如果函数中有“yield”语句,则该函数称为生成器函数。生成器函数为函数调用提供生成器对象,这与带有 return 语句的函数不同。
def generatorFunc():
global numList
numList = range(8)
for i in numList:
print(f"id of {i} is {id(i)}")
yield i*i
mygenerator = generatorFunc()
print(mygenerator)
在上面给出的代码中,输出如下:
<generator object generatorFunc at 0x000001E9692A0BA0>
我理解,当调用函数时,生成器对象是在不经过函数体的情况下创建的。
运行以下代码生成了iterable,并且执行了函数体。
def generatorFunc():
global numList
numList = range(8)
for i in numList:
print(f"id of {i} is {id(i)}")
yield i*i
mygenerator = generatorFunc()
print(mygenerator)
list2 = ([i*i for i in mygenerator])
print(list2)
输出
<generator object generatorFunc at 0x000001E9692A0F90>
id of 0 is 140733860812528
id of 1 is 140733860812560
id of 2 is 140733860812592
id of 3 is 140733860812624
id of 4 is 140733860812656
id of 5 is 140733860812688
id of 6 is 140733860812720
id of 7 is 140733860812752
[0, 1, 16, 81, 256, 625, 1296, 2401]
接下来,当我运行以下代码时,输出有点奇怪。我无法给自己一个满意的解释。
def generatorFunc():
global numList
numList = range(8)
for i in numList:
print(f"id of {i} is {id(i)}")
yield i*i
mygenerator = generatorFunc()
print(mygenerator)
for i in mygenerator:
print(f"value of i is {i}")
print(f"the list is {list(mygenerator)}")
list2 = ([i*i for i in mygenerator])
print(list2)
输出
<generator object generatorFunc at 0x000001E9692A0CF0>
id of 0 is 140733860812528
value of i is 0
id of 1 is 140733860812560
id of 2 is 140733860812592
id of 3 is 140733860812624
id of 4 is 140733860812656
id of 5 is 140733860812688
id of 6 is 140733860812720
id of 7 is 140733860812752
the list is [1, 4, 9, 16, 25, 36, 49]
[]
我不明白为什么“
print(f"value of i is {i}")
”只执行了一次,而print(f"the list is {list(mygenerator)}")
却打印在最后?是不是意味着,一旦“for”使用了“生成器对象”,函数就会从上到下执行,随后“生成器对象”就变成空了?有人可以帮助我理解问题中的最后一个代码以及为什么输出如此?
[1]: Python 中的“yield”关键字有什么作用?
问题出在以下几行代码上:
for i in mygenerator:
print(f"value of i is {i}")
print(f"the list is {list(mygenerator)}")
生成器不会在内存中存储值。当您将生成器转换为列表时,生成器将被耗尽;不再有任何值。因此,循环在运行一次后停止。
此外,只有当生成器被迭代时才会执行生成器的主体,因此给出了输出的顺序。