I have many times screwed up "while True"-loops. When I thought I had a safe exit condition which turned out to be never reached in some rare corner cases. Leading to weird bugs with hanging threads. I have seen colleges screw up in the same way too. Often it is possible to reformulate "while True" to a generator which I think is much preferable (such as in the event loop example). Recursive functions can also be more readable than "while True" because it is easier to make the exit condition explicit. But sometimes they are necessary and then you have to be careful to check that the "while True" always breaks somewhere.
2009/10/10 kj <no.em...@please.post>: > > > > I'm coaching a group of biologists on basic Python scripting. One > of my charges mentioned that he had come across the advice never > to use loops beginning with "while True". Of course, that's one > way to start an infinite loop, but this seems hardly a sufficient > reason to avoid the construct altogether, as long as one includes > an exit that is always reached. (Actually, come to think of it, > there are many situations in which a bona fide infinite loops > (typically within a try: block) is the required construct, e.g. > when implementing an event loop.) > > I use "while True"-loops often, and intend to continue doing this > "while True", but I'm curious to know: how widespread is the > injunction against such loops? Has it reached the status of "best > practice"? > > TIA! > > kynn > -- > http://mail.python.org/mailman/listinfo/python-list > -- mvh Björn -- http://mail.python.org/mailman/listinfo/python-list