while True or while 1

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Dec 12 16:49:02 EST 2010


On Sun, 12 Dec 2010 16:33:41 +0100, Krister Svanlund wrote:

> On Sun, Dec 12, 2010 at 3:14 PM, Max Countryman <maxc at me.com> wrote:
>> I'm sure this has been brought up many times, but a quick Googling
>> didn't yield the decisive results I was hoping for, so I apologize if
>> this has already been addressed in great detail somewhere else.
>>
>> I am wondering what the rationale is behind preferring while True over
>> while 1? For me, it seems that using True provides more clarity, but is
>> that the only benefit? Is while 1 more prone to errors?
> 
> It's just silly to use 1 since it will evaluate to True either way.


With the "while True" idiom in Python 2.x, you can easily exit out of an 
infinite loop without using break:

>>> while True:
...     print "Looping"
...     True = 0
...
Looping
>>>
>>> while True:  # Execute an infinite loop in 0 seconds.
...     print "Looping"
...
>>>


*wink*



-- 
Steven



More information about the Python-list mailing list