I would like to be able to defer the construction of the elements of a list until they are accessed for the first time. The obvious solution (using a generator as below does not work since it is iterable more than once, etc).
For example, the following prints 0 -> 9. I would like to print 0-> 9 twice.
def costly_build_function(i):
return i
def my_function():
return (costly_build_function(i) for i in range(0,10))
tmp = my_function()
# print 0 to 0
for i in tmp:
print i
# print nothing
for i in tmp:
print i