This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Lucas Wiman
Recipients Lucas Wiman, Sumudu.Fernando, eric.araujo, falsetru, rhettinger, terry.reedy, yegle
Date 2016-05-31.20:17:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1464725863.38.0.683869193862.issue10109@psf.upfronthosting.co.za>
In-reply-to
Content
I realize this is an old (and closed!) thread, but here are a few notes from running into this issue recently, working on a search problem with infinite iterators.

First, note that yegle's recursive solution is not correct, since it exhausts the iterators:
>>> def product(*args):
...     if len(args) == 1:
...         for i in args[0]:
...             yield [i]
...     else:
...         for i in args[0]:
...             for j in product(*args[1:]):
...                 j.append(i)
...                 yield j
... 
>>> 
>>> assert len(list(itertools.product(*[iter(range(2)) for _ in range(10)]))) == 2 ** 10
>>> assert len(list(product(*[iter(range(2)) for _ in range(10)]))) == 2 ** 10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError

This is fairly tricky to get right, and even a correct fully-lazy solution requires each iterator to eventually be fully stored in memory. For that reason, Sumudu's MemoryError is not something that can be easily escaped, though it can be delayed.

For infinite iterators, the naive "lexicographic" solution to product(count(0), count(0)) would never get to 1 on the first iterator, yielding (0, 0), (0, 1), (0, 2), ...  To fully explore the space, the user needs to think about how to iterate over that space, so IMO keeping infinite iterators as invalid arguments to itertools.product makes sense.
History
Date User Action Args
2016-05-31 20:17:43Lucas Wimansetrecipients: + Lucas Wiman, rhettinger, terry.reedy, falsetru, eric.araujo, Sumudu.Fernando, yegle
2016-05-31 20:17:43Lucas Wimansetmessageid: <1464725863.38.0.683869193862.issue10109@psf.upfronthosting.co.za>
2016-05-31 20:17:43Lucas Wimanlinkissue10109 messages
2016-05-31 20:17:43Lucas Wimancreate