Basically, because there is absolutely no good reason to use 1 instead of True (well, it saves some negligible amount of typing, I suppose). The only reason it is done that way in C is because C does not have a boolean type. And even in C, it is considered bad practice to say "while (1)" and instead people "#define TRUE 1" (and most OS will already have that #define'd somewhere, actually). In Symbian, there even was '#define FOREVER for(;;)' so you could say

It's a small readability improvement, but it costs nothing, and every little helps. But I digress.

Reasons for not using "while 1:"

It's arbitrary. Why "while 1:" and not "while 2:" or "while 'true'"?

There is something to be said for consistency. As a community, Python programmers decided (or it may have been a decree for Guido, I'm not sure) that "while True" is how we're gonna do infinite loops. Most developers are used to seeing it and when they read code and see "while True", their mind immediately chunks it into a single "forever" token. Because "while 1" is less usual, it draws more attention to itself than it deserves and breaks the flow somewhat.

It's logically incoherent. Logically, "while 1" makes no sense (you have to draw the arbitrary* parallel that 1 is equivalent to truth for it to make sense).

You actually incur a conversion cost in the code as int object 1 needs to be converted to a boolean object. Yes, it's negligible (probably to the point of not being worth mentioning), but still.

While none of the above reasons may seem terribly compelling, you have to keep in mind that there is nothing on the other side of the scales. Since "while 1:" is breaking away from the norm, the question that should be asked is not "why not do it?", but "why do it?"; and there is no good answer to that.

It's a small thing, to be sure, and using "while 1:" is not going to get you mauled by raptors. My comment you've quoted was nested in a series of others addressing the style of the code being discussed. I would not have mentioned it on its own.

* And it is arbitrary. For example, in bash 0 is true (because zero return code indicates successful termination).

My goal was just to compare what we consider best practice (i.e. while myvariable:) with while 1: to see why people feel that the former is the "right" way, whereas the latter is the "wrong" way. (note I'm not saying these are equivalent. I'm saying that they are similar in how we don't explicitly express the Boolean we are testing.)

setrofim wrote:My comment you've quoted was nested in a series of others addressing the style of the code being discussed

Yeah I had considered replying to the thread but didn't want to derail the topic so I figured I would try to query the community. Everything else there I completely agree with, and you are obviously a more experienced programmer, so I wanted to see why some people feel so adamantly about this. I personally like using it and have never really bought the argument that one maintains greater clarity over the other.

Edit: according to this (and I have admittedly not bothered to reproduce the results myself) 1 evaluates to a Boolean faster than True pre-python 3 as a result of True not actually being a true builtin but simply an initially assigned global constant.)

So we can use this an excuse to use "while 1" over "while True" because it faster,not really

If i test Python27,"while 1" is faster,but really just use "while True" and never "while 1".Timeit 10000000 "while 1" in 0.24 secTimeit 10000000 "while True" in 0.43 sec

Interesting; seems I was wrong about which is faster. Thanks Mekire for posting the link, and snippsat for verifying it. OK, I guess you can strike the last point from my list . I really should know better than to make performance claims based on logic, rather than evidence... The rest of my argument stands though.