[Python-Dev] A `cogen' module - an observation

Oren Tirosh oren-py-d@hishome.net
Tue, 27 Aug 2002 02:27:40 -0400

On Tue, Aug 27, 2002 at 01:56:36AM -0400, Raymond Hettinger wrote:
> From: "Oren Tirosh" <oren-py-d@hishome.net>
> > [f(x, y) for x in X for y in Y] 
> > 
> >   is equivalent to:
> > 
> > [f(x, y) for x, y in cartesian(X, Y)] 
> Is the order guaranteed to be the same?


Combinatorial generators.

All generators below have the property of yielding successive results
in sorted order, given than input sequences were already sorted.

> Will each work the same for a non-restartable
> iterator, say a file object (equivalently put,
> does the second one read Y once or many times)?

They have exactly the same re-iterability wart as nested loops or list 
comprehensions - an exhausted iterator is indistinguishable from an empty 

> Would Descartes object to his name being used thusly?

The cartesian product is a set operation and therefore has no defined 
order. When generating it you need some specific order and this one makes
the most sense. If you use it with a 'nested loop' mindset instead of a
'set theory' mindset Rene would have had some grounds for objection :-)