Loop-and-a-half (Re: Curious assignment behaviour)

John Roth johnroth at ameritech.net
Thu Oct 11 21:40:41 CEST 2001

"Greg Ewing" <greg at cosc.canterbury.ac.nz> wrote in message
news:3BC4E625.E6C73878 at cosc.canterbury.ac.nz...
> Paul Rubin wrote:
> >
> > I feel seriously cramped in Python when I have to say
> >
> >   while 1:
> >      x=get_next()
> >      if not x: break
> >      whatever(x)
> I feel the solution to this is *not* to go in for
> any sort of assignment-in-expressions hackery, but
> to provide a decent loop-and-a-half control structure.
> My current idea for this is
>   while:
>     x = get_next()
>   gives x:
>     whatever(x)

The notion that while is adequate as a looping construct
comes from the minimalist school. From a techincal
point, they are right, but from a pragmatic point of writing
economical, understandable programs, they are wrong.

If you're going to expand the loop syntax, all four (or five)
parts should be explicit: initialization, test, reinitialization,
body and possibly zero-trip exception. In particular,
test should allow more than one expression: it should allow
either a single expression or any number of statements. In
this case, exit would require an explicit break.

Unfortunately, this gets to be more than a bit cumbersome.

John Roth
> --
> Greg Ewing, Computer Science Dept, University of Canterbury,
> Christchurch, New Zealand
> To get my email address, please visit my web page:
> http://www.cosc.canterbury.ac.nz/~greg

More information about the Python-list mailing list