B
B
burdakovd2011-08-31 23:51:30
Python
burdakovd, 2011-08-31 23:51:30

Insidious Python generators?

numbers = range(10)
squares = (x * x for x in range(10))
fours = (x * x for x in squares)
zip(squares, fours)

What will the code output? The result will be "[(0, 1), (4, 81), (16, 625), (36, 2401
), (64, 6561)]
" generators. Usually, the case of generator reuse is immediately detected - because. the second time it returns an empty sequence, but in this case the empty sequence is not returned, because the generator is read "twice" in parallel by zip. Also: sandersn.com/blog//index.php/2009/06/29/python_s_i...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question