Re:“I'm basically giving myself a permanent vacation from be...
by blake_himself on Friday July 13, 2018 @12:37AM
(#17520537)
[ Parent
| on HN ]
Re:“I'm basically giving myself a permanent vacation from be...
by Delgan on Friday July 13, 2018 @03:36AM
(#17521139)
On the contrary, loop-and-a-half is intended to avoid repeating stuff outside and inside of the loop body.
while true:
x = stuff
if not x:
break
Re:“I'm basically giving myself a permanent vacation from be...
by chucksmash on Sunday July 15, 2018 @12:57AM
(#17533866)
GPs example is perfectly valid as well - this is just a restatement of the same logic in a way that keeps the logic contained within the loop at the cost of using a conditional plus a break inside of an unconditional loop. See [0]:
Another motivating code pattern is the "loop and a half".
It was once common for processing a file by line, but that
has been solved by making file objects iterable; however
other non-iterable interfaces still suffer from patterns
like:
line = f.readline()
while line:
... # process line
line = f.readline()
or like this:
while True:
line = f.readline()
if not line:
break
... # process line
Either of those could be replaced with a much more clear
and concise version using an assignment expression:
while line := f.readline():
... # process line
[0]: https://lwn.net/Articles/757713/