while True or while 1

Grant Edwards invalid at invalid.invalid
Mon Jan 23 10:51:37 EST 2012


On 2012-01-21, Erik Max Francis <max at alcyone.com> wrote:
> Chris Angelico wrote:
>> On Sun, Jan 22, 2012 at 12:47 AM, Andrea Crotti
>> <andrea.crotti.0 at gmail.com> wrote:
>>> So I tried to do the following, and the result is surprising.  For what
>>> I can see it looks like the interpreter can optimize away the 1 boolean
>>> conversion while it doesn't with the True, the opposite of what I
>>> supposed.
>>>
>>> Anyone can explain me why is that, or maybe is my conclusion wrong?
>> 
>> In Python 3, they compile to the same code, because 'True' is a
>> keyword. In Python 2, you can reassign True to be 0.
>
> Why this should concern anyone, I don't know; 

I don't think it does concern anybody (except the compiler, who treats
all identifiers the same).

[...]

> The real reason people still use the `while 1` construct, I would 
> imagine, is just inertia or habit,

That's certain why I do it.  It's left over from the days when C and
Python didn't have symbolic boolean "constants".

> rather than a conscious, defensive decision.  If it's the latter,
> it's a case of being _way_ too defensive.

-- 
Grant Edwards               grant.b.edwards        Yow! Hmmm ... A hash-singer
                                  at               and a cross-eyed guy were
                              gmail.com            SLEEPING on a deserted
                                                   island, when ...



More information about the Python-list mailing list