[Tutor] Ending a loop with a condition not at the top

Ben Finney ben+python at benfinney.id.au
Mon Jul 21 05:08:27 CEST 2014

Marc Tompkins <marc.tompkins at gmail.com> writes:

> And I apologize for imputing motive (a liking for "while True"); I'd
> just noticed that you often advise it. I don't know who _does_ think
> this is a desirable pattern; I'd love to hear their argument for it -
> it must be really good.

It works better than alternatives already available in the language; it
is simpler than alternatives already available in the language. It isn't
particularly ugly (nor is it particularly beautiful, I'll grant you).

And it works well enough, and is simple enough, to be recommended
without caveat to anyone needing a loop to end somewhere other than the

Any alternative structure would need to be *significantly* better than
what already works now. That, in the end, is the strongest argument for
‘while True: … break’.

 \          “When [science] permits us to see the far side of some new |
  `\      horizon, we remember those who prepared the way – seeing for |
_o__)                          them also.” —Carl Sagan, _Cosmos_, 1980 |
Ben Finney

More information about the Tutor mailing list