Hi

I see two issues. The first is present behaviour. The second is alternative
ways of ordering the elements of a Cartesian product.

Here's an example of the present behaviour.

    >>> iter_range = iter(range(100))
    >>> prod = itertools.product(iter_range, "abcdef")
    >>> next(iter_range)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    StopIteration

It's not what I expected before I read this thread, and it is what I
expected after I read this thread. I regard it as a bug. I expect iterators
to be lazy rather than greedy when consuming input. This helps make for
efficient and non-blocking pipelines. I see no reason for the product to
consume any items until it has been asked to yield a member of the product.

The second issue is that there are at least three sensible ways to iterate
a Cartesian product. The itertools.product function is, as said in the
docs, equivalent to nested for loops. On ordered input it's equivalent to
lexicographic ordering.

The squares on a chessboard are labelled a1, a2, a3, .... b1, b2, b3, ... ,
g7, g8 (using lexicographic order. There are as many fractions as there are
counting numbers. The usual way to prove this is to order unsimplified
fractions p/q via the TRIPLE (p + q, p, q). This gives a diagonal ordering.
The third order is via subsquares. In other words order via the TRIPLE
(max(p,q), p, q).

I suggest on the basis of batteries included that all three methods be
included in itertools, perhaps with names lexicproduct, diagproduct and
maxproduct. Finally, product is a synonym for lexicproduct. This allows the
programmer to say without writing a comment that they've considered the
alternatives and chosen the function also known as product.

with best regards

Jonathan
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/SF3X2LJE7BWVECW5NQRVQI3XZU7PRBLN/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to