while expression feature proposal

Paul Rubin no.email at nospam.invalid
Fri Oct 26 23:43:55 EDT 2012


Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:
> There's no need for it *inside* of while expressions. It doesn't add to 
> the expressiveness of the language, or increase the power of the 
> language, or help people write correct code. It saves one trivial line of 
> code in some, but not all, while loops, at the cost of increasing the 
> complexity of the language and parser.

I'm maybe +0.25 on the suggestion but it does save more than one line in
the places where it's useful, plus improves clarity.  You get to write
 
   while (foo() as x) is not None:
       ...

instead of

  while True:
    x = foo()
    if x is not None:
       break
    ...

which is much uglier.  Maybe there are even times when you want

   while (left() as x) != (right() as y): ...

that is even messier when expanded out.

There was also the cascaded regexp match example, that happens regularly
in real code and that I've hacked various workarounds for, or wished for
a Maybe monad.



More information about the Python-list mailing list