5 ms·
As a huge Python fan, I'm ashamed to admit but I don't get the while True: break What's the problem? I supose the use case is while True:
by Vaskivo 13y ago
As a huge Python fan, I'm ashamed to admit but I don't get the
while True:
break
What's the problem? I supose the use case is
while True:
# do stuff
if some_condition:
break
What is the alternative? 'while some_condition'? That means we must have the 'some_condition' variable outside of the loop. And if we have multiple exit points it may become a mess.
- herge 13y agoAlso an easy way to fake a do ... while loop.
- roywiggins 13y agoIf there is a simple condition I prefer "while condition" because it makes the code easier to read: I can tell (roughly) when the loop is supposed to exit without reading the contents of the loop and scanning for a break.
- dec0dedab0de 13y agoThat means we must have the 'some_condition' variable outside of the loop. I'm not sure what you mean by that, this works just fine. Though you would probably use range in the real world. i=0 while i !=5: i+=1 Instead of i=0 while True: i+=1 if i == 5: break
- klapinat0r 13y agoHe means the variable used in the condition to break (or start/not start), i.e. "i" in your example - which you have outside of your loop.
- dec0dedab0de 13y agoOK, that makes sense then. I was thinking "the condition" was the variable changing within the loop.
- wiredfool 13y agoI think this is more for: while True: result = do_something() if not result: break rather than: result = True while result: result = do_something() [[edit apparently tab ret submits, not whatever I was trying to do with the actual editing]]
- collyw 13y agoI prefer the second one and its a line less code.
- falcolas 13y agoPersonally, because I find infinite loops to be a real PITA, I prefer to do: for _ in xrange(100000): break else: logging.error("ran into an infinite loop") unless I really do need an infinite loop for things like event handler loop, which is admittedly quite rare.
- untitaker_ 13y agoDoesn't it bother you that this code is technically completely wrong?
- meowface 13y agoThat seems incredibly silly, and looks like it could lead to very infrequent bugs (the worst kind). `while True` is shorter, simpler, and conveys the actual purpose better. Maybe having a statement like that when testing is okay, but in production code that looks insane.
- falcolas 13y agoI'd argue that `while True` would be fine in testing, but problematic for production, particularly in the types of programs I write most frequently (long running, minimal supervision). In these daemons, stalling on infinite loops is significantly more painful than dropping out of a loop early occasionally (and with proper logging that it did fall out of the loop, to boot). It's certainly not as idiomatic, but it's more correct in the long run. My eyes were opened to this when reading through the NASA C guidelines. Closing the door on infinite loops lets programs recover gracefully and do the correct thing for the duration of the programming, as opposed to thing that may be correct for that moment of operation: i.e. looping 1-2 more times beyond the limits of the xrange counter.