原文
What does the “yield” keyword do?
翻译
To understand what
yield
does, you must understand what generators are. And before generators come iterables.
为了理解yield的作用,我们必须先理解生成器,而在生成器之前必须先理解迭代器
迭代器
When you create a list, you can read its items one by one. Reading its items one by one is called iteration:
1
2
3
4
5
6 1, 2, 3] mylist = [
for i in mylist:
print(i)
1
2
3
当我们创建一个list,我们只能一个个读取list,而挨个读取的这个过程就被称为迭代
mylist
is an iterable. When you use a list comprehension, you create a list, and so an iterable:
1
2
3
4
5
6 for x in range(3)] mylist = [x*x
for i in mylist:
print(i)
0
1
4
mylist就是一个迭代器,当我们使用列表推导时,也就创造了一个list,或者说是一个迭代器
more >>