Is there a good reason why there isn't a do while flow control statement in python?
Why 开发者_开发技巧do people have to write while
and break
explicitly?
It has been proposed in PEP 315 but hasn't been implemented because nobody has come up with a syntax that's clearer than the while True
with an inner if-break
.
Probably because Guido didn't think it was necessary. There are a bunch of different flow-control statements you could support, but most of them are variants of each other. Frankly, I've found the do-while statement to be one of the less useful ones.
Because then you would have two ways to do something.
Python adds features only when they significantly simplify some code.
while True:
...
if not cond: break
is not less simple than a do-while loop, for which there is no obvious natural python syntax anyway.
do:
...
while cond
(Looks weird)
or this?
do:
...
while cond
(The while looks like a regular while statement)
精彩评论