while True or while 1
Erik Max Francis
max at alcyone.com
Sat Jan 21 16:13:14 EST 2012
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; someone who's rebound
`True` or `False` to evaluate to something other than true and false,
respectively, is only doing so to be difficult (or very foolish). One
of the principles of Python programming is that We're All Adults Here,
so this kind of defensive programming is really superfluous. In other
words, yes, it's quite reasonable to assume that (even in Python 2)
`True` is bound to something which is, in fact, true.
The real reason people still use the `while 1` construct, I would
imagine, is just inertia or habit, rather than a conscious, defensive
decision. If it's the latter, it's a case of being _way_ too defensive.
--
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Jabber erikmaxfrancis
Ambition can creep as well as soar.
-- Edmund Burke
More information about the Python-list
mailing list