Why the "while 1:" hate?

A forum for general discussion of the Python programming language.

Why the "while 1:" hate?

Postby Mekire » Wed Mar 27, 2013 8:20 am

setrofim wrote:Use "while True:" rather than "while 1:"; this ain't C.

I have never understood why some people take issue with this. All but the most novice python programmers understand that while 1 is an infinite loop, and I have never personally felt it to be ugly.

I have always thought that the fact that every python object can be resolved to true or false actually lent itself to these types of things.

Most people generally write:
Code: Select all
while myvariable:
as opposed to
Code: Select all
while myvariable == True
(and from my understanding the former is actually better performance-wise) so why is this considered bad form.

I understand we have the "Explicit is better than implicit," maxim, but I have, as I said, always felt while 1 was more than obvious enough.

Just wondering why some people hold to this.
Cheers,
-Mek
User avatar
Mekire
 
Posts: 828
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Why the "while 1:" hate?

Postby setrofim » Wed Mar 27, 2013 9:04 am

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
Code: Select all
FOREVER
    {
    // do stuff;
    }

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).
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Why the "while 1:" hate?

Postby stranac » Wed Mar 27, 2013 10:22 am

Mekire wrote:Most people generally write:
Code: Select all
while myvariable:
as opposed to
Code: Select all
while myvariable == True
(and from my understanding the former is actually better performance-wise) so why is this considered bad form.

It's not.
The former is actually the recommended way to write it.
The later might even be wrong, depending on data, since not every value that evaluates to True in boolean context will be equal to True.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 907
Joined: Thu Feb 07, 2013 3:42 pm

Re: Why the "while 1:" hate?

Postby Mekire » Wed Mar 27, 2013 12:59 pm

stranac wrote:The former is actually the recommended way to write it.
Sorry my language was ambiguous there. I wasn't asking if that particular part was bad form; I was comparing the use of a variable for the Boolean with using
Code: Select all
while 1:
and asking why just using a variable was considered clear whereas using 1 was considered bad form.

But yes you are correct, my example was bad. I suppose the appropriate comparison would have been with:
Code: Select all
while bool(myvariable):
(which is obviously silly).

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.

-Mek

(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.)
http://stackoverflow.com/questions/3815359/while-1-vs-for-whiletrue-why-is-there-a-difference
User avatar
Mekire
 
Posts: 828
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Why the "while 1:" hate?

Postby snippsat » Wed Mar 27, 2013 4:47 pm

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 :lol:

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 sec
Timeit 10000000 "while True" in 0.43 sec
User avatar
snippsat
 
Posts: 90
Joined: Thu Feb 21, 2013 12:04 am

Re: Why the "while 1:" hate?

Postby setrofim » Wed Mar 27, 2013 5:26 pm

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.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Why the "while 1:" hate?

Postby pydsigner » Sat Mar 30, 2013 11:20 pm

When I saw the title of this thread, I felt that I had to jump in and say that I like while 1 better too, besides the fact that it's faster. :D
Desktop: Linux Mint 15, 8GB RAM, 500GB HDD, 2.4GHz Xeon
Laptop: Win8, 8GB RAM, 1TB HDD, 1.6GHz 4th gen i5
Python: 2.7.4

For God sent not His Son into the world to condemn the world, but that the world through Him might be saved! (John 3:17)
pydsigner
 
Posts: 20
Joined: Sat Feb 23, 2013 12:17 am
Location: Midwestern United States


Return to General Discussions

Who is online

Users browsing this forum: Google [Bot] and 3 guests