The rap against "while True:" loops
Jaime Buelta
jaime.buelta at gmail.com
Tue Oct 20 01:39:52 EDT 2009
For me, it's more a question of clarity than anything else. I don't
like very much using break, continue or more than one return per
function on C/C++, but sometimes it's much clearer to use them.
Also, in Python I use them often, as usually the code is cleaner this
way.
for example, I will wrote that code in C/C++
for (i=0;(i<MAX) && (get_out == True);i++) {
..... do lot of things...
....
....
if( condition) {
get_out = True
}
}
but in Python will use
for i in range(MAX):
..do lot of things...
if condition:
#Exit the loop
break
Don't know, seems to me that the more syntetic code of Python helps me
to see clearly when to exit, in C/C++ the break statements seems to
confuse me. Probably related with the amount (and density) of code
I think an infinity loop (while True:) should be used only on, well,
infinite loops (or at least indeterminate ones that depends on
arguably input, like user input or network data) I wouldn't use them
for reading a file, for example...
But, anyway, I think the key concept is to use them when it's more
readable and makes more sense than a "finite loop". By the way, "more
readable" to me, of course :-P
More information about the Python-list
mailing list