# Re: while True or while 1

Discussion in 'Python' started by Krister Svanlund, Dec 12, 2010.

1. ### Krister SvanlundGuest

On Sun, Dec 12, 2010 at 3:14 PM, Max Countryman <> 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.

Krister Svanlund, Dec 12, 2010

2. ### Steven D'ApranoGuest

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

> On Sun, Dec 12, 2010 at 3:14 PM, Max Countryman <> 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
>>
>> 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

Steven D'Aprano, Dec 12, 2010

3. ### Grant EdwardsGuest

On 2010-12-12, Steven D'Aprano <> wrote:

> 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*

Sadly, I've seen people do stuff like that in real programs...

--
Grant Edwards grant.b.edwards Yow! I was making donuts
at and now I'm on a bus!
gmail.com

Grant Edwards, Dec 13, 2010
4. ### Gregory EwingGuest

Steven D'Aprano wrote:

>>>>while True:

>
> ... print "Looping"
> ... True = 0

Just remember that if you use that inside a function, you'll
have to initialise True to True before... er, wait a moment,
that won't work... ah, I know:

def f(true = True):
True = true
while True:
...
True = False

--
Greg

Gregory Ewing, Dec 14, 2010
5. ### Hans-Peter JansenGuest

On Tuesday 14 December 2010, 10:19:04 Gregory Ewing wrote:
> Steven D'Aprano wrote:
> >>>>while True:

> >
> > ... print "Looping"
> > ... True = 0

>
> Just remember that if you use that inside a function, you'll
> have to initialise True to True before... er, wait a moment,
> that won't work... ah, I know:
>
> def f(true = True):
> True = true
> while True:
> ...
> True = False

Thankfully, with Python 3 this code falls flat on its face.

If I would have to _consume_ code like that more often,
it would require me to also use a vomit resistant keyboard cover..

Pete

Hans-Peter Jansen, Dec 14, 2010
6. ### Kurt MuellerGuest

Am 14.12.2010 11:33, schrieb Hans-Peter Jansen:
> On Tuesday 14 December 2010, 10:19:04 Gregory Ewing wrote:
>> Steven D'Aprano wrote:
>>>>>> while True:
>>>
>>> ... print "Looping"
>>> ... True = 0

>>
>> Just remember that if you use that inside a function, you'll
>> have to initialise True to True before... er, wait a moment,
>> that won't work... ah, I know:
>>
>> def f(true = True):
>> True = true
>> while True:
>> ...
>> True = False

>
> Thankfully, with Python 3 this code falls flat on its face.
>
> If I would have to _consume_ code like that more often,
> it would require me to also use a vomit resistant keyboard cover..
>
> Pete

True yesterday, today and in the future:

Yesterday:
"Pilate said to him, True? what is true?
Having said this he went out again to the Jews
and said to them, I see no wrong in him."

Today:
We are so thankful that today we are free
to define "True" ourselves using Python 2.x.

Future:
Be warned, the future gets darker!

;-)

GrÃ¼essli
--
Kurt Mueller

Kurt Mueller, Dec 14, 2010
7. ### Arnaud DelobelleGuest

Gregory Ewing <> writes:

> Steven D'Aprano wrote:
>
>>>>>while True:

>>
>> ... print "Looping"
>> ... True = 0

>
> Just remember that if you use that inside a function, you'll
> have to initialise True to True before... er, wait a moment,
> that won't work... ah, I know:
>
> def f(true = True):
> True = true
> while True:
> ...
> True = False

You also need to initialise False to False for it to be really
robust. So something like this will do.

True = not 0
False = not True
while True:
...
True = False

--
Arnaud

Arnaud Delobelle, Dec 14, 2010