while (a=b()) ...

Tim Peters tim_one at email.msn.com
Sat May 15 14:57:47 EDT 1999


[Donn Cave]
> (best I can do is  "i = 0; while 1: i = i + 1; ...")

[Tim]
> You could try
>
>     for i in xrange(sys.maxint):
>
> instead, or do
>
>     perpetuity = xrange(sys.maxint)
>     ...
>     for i in perpetuity:

[Jim Meier]
> 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)

Jim, you may have missed that the above was not a proposal -- it works
today.  "perpetuity" is indeed not a list or set, but it is a sequence, and
sequences are what "for" chews on:

Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
IDLE 0.5 -- press F1 for help
>>> import sys
>>> perpetuity = xrange(sys.maxint)
>>> for i in perpetuity:
        print i,


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
Traceback (innermost last):
  File "<pyshell#4>", line 2, in ?
    print i,
  File "PyShell.py", line 629, in write
    self.shell.write(s, self.tags)
  File "PyShell.py", line 620, in write
    raise KeyboardInterrupt
KeyboardInterrupt:
>>>

For all the rest, it's been debated repeatedly over most of the 90's.  That
doesn't mean it shouldn't be debated again, but it does mean a number of
people who sat thru the last 10 iterations are likely to play with
diminished enthusiasm this time around <wink>.

> ...
> 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.

The latter is what Python lacks today, and the source of the frequent

    for i in range(len(seq)):
        element = seq[i]

idiom.

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

Still doesn't give you a cursor, though.  Python *has* one, of course, under
the covers.  The intent of "i in *" is to expose it.

> 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.

But already has a different meaning:

>>> for (x, y) in ("ab", "cd"):
        print x, y

a b
c d
>>>

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

Most suggestions are indeed non-problems because they try to assign new
semantics to syntax that already means something else; dead on arrival.
Proposals don't try to overload semicolons and keywords because they think
it's pretty <wink>.

BTW, I'm one of those not bothered by "while 1:".  With a little
imagination, those who are can instead write e.g.

    while "more input remains":
        line = f.readline()
        if not line:
            break
        process(line)

"more input remains" as eternally true as 1 is <wink>.

never-met-a-loop-he-didn't-like-ly y'rs  - tim






More information about the Python-list mailing list