while (a=b()) ...

Jim Meier fatjim at home.com
Sat May 15 13:55:10 EDT 1999


Tim Peters wrote:

> [Donn Cave]
> > ...
> > There might be some broader mileage in the construct, for example
> > "for i:" might be taken to mean "for i in range(INF):", which is a
> > little awkward in the current language (best I can do is  "i = 0;
> > while 1: i = i + 1; ...")
>
> You could try
>
>     for i in xrange(sys.maxint):
>
> instead, or do
>
>     perpetuity = xrange(sys.maxint)
>     ...
>     for i in perpetuity:
>
> Ya, ya.

Well, I kinda gawk at that. My gut says "ick!" because of the idea of
iterating over infinity.. "for" in python can often be synonymous with
"for each",  so "for each in in perpetuity" is meaningless.. perpetuity is
not a list or a set! Other than that, I think it's very neat.. how about
an
>>>in perpetuity:
...    (stuff that is actually never intended to terminate)


"for i:", however, is just wrong and misleading about the meaning of
"for". See below for why...

>
> A while back we hashed out a spelling for parallel iteration, as in:
>
>     for x in xlist; y in ylist:
>         print "I'm printed min(len(x), len(y)) times."
>
> A generalization, sneaking the old "indexing" proposal back in under a
> different spelling:
>
>     for i in *; x in xlist:
>         print "The value of x[%d] is %s" % (i, x)
>
> A degeneration:
>
>     for i in *:
>         print "perpetuity"
>
> foreverly y'rs  - tim

This use of "*" is nicer, but not legible.. "*" is a shell term or a geek
term, but not a fluid english term. It's not apparent what it means by
looking at it unless. (Don't try to convince me otherwise by saying "we
all know what it means!", because we mostly all also know things like #
for hash and such... :)

This parallel list idiom brings me back to why "for i:" is
meaningless(sorta :) : "For" means iteration. It can generally be
translated into "for each" (except where it can't). "for each i:" is
meaningless and/or undefined: does it mean take the current i, do nothing
with it, and loop once? If i is a list, should it iterate over the list,
and if so, what is the cursor variable? Or, is it a new feature of
computer language, grown out of python's legibility and similarity to
natural languge: an exclamation: "for Pete's_sake!" that of course would
mean a special case parsing for the spark symbol, and i'm not sure about
the bang. Baaa-aa-aad. :->

So, for a "for each" to be meaningful, we need a. sequences(or generators)
to iterate over, and cursors to represent the current value of the
iteration. Which is why I like

>    for x in xlist; y in ylist:
>        print "I'm printed min(len(x), len(y)) times."

greatly. Although I would try to make it more "pythonic" in my mind by
expressing it as
>    for (x,y) in (xlist, ylist):
>        print print "I'm printed min(len(x), len(y)) times."

, although that's a little less legible.

Can we make "for" into "for each"? It is terminology many are used to from
mathematics:

for each (tasty_treat,horrible_fate) in (chocolates, terrible_pains):
    print "customer: What's this yummy looking "%s? I think I'll try it.
Oh God! I've been %s" % tasty_treat, horrible_fate
    print "shopkeeper: Do you like it? That's our very best %s-ing %s!!!"
% horrible_fate, tasty_treat

(ok, that's not math. but the examples were getting stale.)


What would happen in this version of for if one of the sequences were a
sequence of sequences? would it be good form to do:
def questionable_syntax():
    if random_value_0_to_1 < 1:
        return 1
    else:
        return (2,3)

for each (a, b) in (questionable_syntax, [1,2,3,4]):
    dance_and_entertain(a,b)

...? Or is that a non-problem?

-Jim.







More information about the Python-list mailing list