首先,假设大家都对于pytyhon的List comprehension的使用有了一定经验(它可以用于list,set,和dict哦)
不熟悉的参考介绍: Comprehending Python’s Comprehensions – dbader.org
generator
generator是哦嗯一种返回lazy iterator的特殊类型函数。list comprehensions return full lists, while generator expressions return generators.
如下面代码,分别使用list和generator的构造,但是两者所占据的内存大家却完全不一样。
>>> import sys
>>> nums_squared_lc = [i ** 2 for i in range(10000)]
>>> sys.getsizeof(nums_squared_lc)
87624
>>> nums_squared_gc = (i ** 2 for i in range(10000))
>>> print(sys.getsizeof(nums_squared_gc))
120
generator functions are a special kind of function that return a lazy iterator. These are objects that you can loop over like a list. However, unlike lists, lazy iterators do not store their contents in memory.
在python中,通过“类似list comprehension”或者yield的使用,我们可以构造generator并调用。
yield
yield的作用介绍如下,当使用generator的特殊方法,比如next()时,generator中的代码会被执行到yield,当经过yield时,python中generator的执行会被中断,函数的当前状态会被保存,直到下一次执行。
下列函数是yield的一个经典用法。
def infinite_sequence():num = 0while True:yield numnum += 1
在调用该函数的过程中,每一次,输出的数会增加1,相比于return,yield会保存function当前的状态,参考How to Use Generators and yield in Python – Real Python
On the whole,
yield
is a fairly simple statement. Its primary job is to control the flow of a generator function in a way that’s similar toreturn
statements. As briefly mentioned above, though, the Python yield statement has a few tricks up its sleeve.When you call a generator function or use a generator expression, you return a special iterator called a generator. You can assign this generator to a variable in order to use it. When you call special methods on the generator, such as
next()
, the code within the function is executed up toyield
.When the Python yield statement is hit, the program suspends function execution and returns the yielded value to the caller. (In contrast,
return
stops function execution completely.) When a function is suspended, the state of that function is saved. This includes any variable bindings local to the generator, the instruction pointer, the internal stack, and any exception handling.This allows you to resume function execution whenever you call one of the generator’s methods. In this way, all function evaluation picks back up right after
yield
. You can see this in action by using multiple Python yield statements:
When you use
next()
, Python calls.__next__()
on the function you pass in as a parameter. There are some special effects that this parameterization allows, but it goes beyond the scope of this article. Experiment with changing the parameter you pass tonext()
and see what happens!