[Python-Dev] second experiment

Thomas Wouters thomas@xs4all.net
Wed, 12 Jul 2000 14:44:00 +0200


On Wed, Jul 12, 2000 at 08:10:52AM -0400, Greg Wilson wrote:

> (A) for x in [10, 20, 30]; y in [1, 2, 3]:
>         print x+y
> 
> (B) for (x,y) in zip([10, 20, 30], [1, 2, 3]):
>         print x+y
> 
> (C) for (x in [10, 20, 30]) and (y in [1, 2, 3]):
>         print x+y

Hm, grouping them... Hadn't thought of that. That's going to be gruesome to
implement, but it might just be possible. It isn't possible to use this
'and'-syntax without parentheses, in any case.

> (D) something else

for x in [10, 20, 30] while y in [1, 2, 3]:
	print x+y

for x; y in [10, 20, 20]; [1, 2, 3]:
	print x+y

> to their output:

I'd suggest additional output, too:


4)
  1
  2
  3

5)
  10 
  20 
  30

And tell them they can match more than one construct to the same output ;-)

> 2. Should the lists be the same length, or different lengths?  I think the
>    latter is the trickier case (equally reasonable to say "iterate for
>    length of shortest" and "error if lists' lengths unequal") --- is that
>    worth testing?

Probably... I'm not sure howmuch time you want to keep them busy. This is
supposed to be intuitive, which means they shouldn't be pressured to find
the right answer. 

> 3. Can someone provide a couple of list comprehension alternatives as
>    well?

I'm not going there for a while. I'm dazed and confused about list
comprehensions, that's for sure. Well, you can try seeing whether they think
one of these should be zipped or cartesian product:

[ x,y for x in [1, 2, 3] for y in [10, 20, 30] ]
[ x;y for x in [1, 2, 3]; for y in [10, 20, 30] ]

No use checking for commas, we'd have to re-write Pythons' parser or
Grammar entirely to allow that.

See my upcoming post on the various proposed alterantive syntaxes and why
they aren't going to work ;-)

-- 
Thomas Wouters <thomas@xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!