0

I have a python script, in which I do the following at one point:

iterator = product(*izip(repeat(0,len(myList)),myList))

I use the python command line, and do execfile('myScript.py'). After the code has run, I try the following:

>>> iterator
<itertools.product object at 0x182f06370>
>>> list(iterator)
[]

However, if I place a print statement in the script print list(iterator) right after iterator has been generated, the code prints the elements of the list correctly.

Why do the elements disappear after the file execution is done? Is this normal behavior?

sodiumnitrate
  • 2,899
  • 6
  • 30
  • 49

1 Answers1

2

From the python docs:

Iterator is an object representing a stream of data. Repeated calls to the iterator’s next() method return successive items in the stream. When no more data are available a StopIteration exception is raised instead. At this point, the iterator object is exhausted and any further calls to its next() method just raise StopIteration again.

Igor Pejic
  • 3,658
  • 1
  • 14
  • 32