[Python-Dev] re: list comprehension / pop quiz

esr@thyrsus.com esr@thyrsus.com
Wed, 12 Jul 2000 11:49:45 -0400

Guido van Rossum <guido@beopen.com>:
> What do you think the following code would print?
>     for i in (10, 20, 30); j in (1, 2, 3):
>         print i+j

I think thre confusion over ambiguous syntax is telling us that what we
need here is to bite the bullet and add a keyword to the language so
there is a clear distinction from existing concepts.

I make so bold as to sugest this:

     for i in (10, 20, 30) with j in (1, 2, 3):
         print i+j

However, I have to say that having meditated on this design problem at some
length I now think proliferating new control structures is the wrong answer.
Instead, I propose that we look for ways to make smarter data structures
drive control.

Concretely. this approach would make `with' a data contructor, throw
out the parallel-loop syntax, and write the following:

>>> [2] with [3]
[(2, 3)]

>>> (10, 20, 30) with (1, 2, 3)
((10, 1), (20, 2), (30, 3))

>>> for (i, j) in (10, 20, 30) with (1, 2, 3): print i + j

This doesn't instantly give us a way to interpret 

for (i, j) in (10, 20, 30, 40) with (1, 2): print i + j

But suppose that the semantics of `with' were that it joins parallel
arguments as far as it can and stops whenever it runs out of elements
in any one argument.  Then,

>>> (10, 20, 30, 40) with (1, 2)
((10, 1), (20,2))

Problem solved.

Now let's make life easier.  The `with' keyword can actually become an
n-adic intrinsic function, analogous to `range'.  Now we write

>>> for (i, j) in with((10, 20, 30, 40), (1, 2)): print i + j

Presto! All need for parallel loops has now disappeared.  If anyone
doubts this, I will cheerfully produce source code for with().
		<a href="http://www.tuxedo.org/~esr">Eric S. Raymond</a>

It is proper to take alarm at the first experiment on our
liberties. We hold this prudent jealousy to be the first duty of
citizens and one of the noblest characteristics of the late
Revolution. The freemen of America did not wait till usurped power had
strengthened itself by exercise and entangled the question in
precedents. They saw all the consequences in the principle, and they
avoided the consequences by denying the principle. We revere this
lesson too much ... to forget it
	-- James Madison.