It doesn't store intermediate results, but it has to store the input values because each of those might be needed several times for several output values.
Since you can only iterate once over an iterator, product
cannot be implemented equivalent to this:
def prod(a, b):
for x in a:
for y in b:
yield (x, y)
If here b
is an iterator, it will be exhausted after the first iteration of the outer loop and no more elements will be produced in subsequent executions of for y in b
.
product
works around this problem by storing all the elements that are produced by b
, so that they can be used repeatedly:
def prod(a, b):
b_ = tuple(b) # create tuple with all the elements produced by b
for x in a:
for y in b_:
yield (x, y)
In fact, product
tries to store the elements produced by all the iterables it is given, even though that could be avoided for its first parameter. The function only needs to walk over the first iterable once, so it wouldn't have to cache those values. But it tries to do anyway, which leads to the MemoryError
you see.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…