The rap against "while True:" loops

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Wed Oct 14 19:10:48 EDT 2009


On Wed, 14 Oct 2009 18:30:06 +0100, Tim Rowe wrote:

> 2009/10/14 Dennis Lee Bieber <wlfraed at ix.netcom.com>:
> 
>>        If anything -- I'd suggest a proposal to add a plain    loop
>>           as a
>> keyword in Python, whose effect is equivalent to a "while True", but a
>> break    must be used to exit said loop (well, we'll ignore raising an
>> exception <G>)
> 
> And with enough static analysis to guarantee that the break will be
> reached? I think it would be a bit much to expect Python to solve the
> halting problem!

That's a stupid objection. Python doesn't guarantee that any of the 
following will halt:

for x in iterator:
    pass

while flag:
    pass

for x in [1, 10, 20, 10**100]:
    time.sleep(x)

(Technically, that last one will eventually halt, if you're prepared to 
wait long enough... about a billion trillion trillion trillion trillion 
trillion trillion trillion years.)


Why should Python make that guarantee about this hypothetical "loop 
forever" construct?





-- 
Steven



More information about the Python-list mailing list