Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Iterator: amazing behavior

I came across the following example:

  def count(start=0):
      num = start
      while True:
          yield num
          num += 1

When I try to apply it (Sage 8.6), the result is different than I expected:

print [count().next() for i in range(18)]
print next(count())
print next(count())

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
0
0

Can someone please elucidate why the output is not equal to:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
0
1