Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why avoid while loops?

Tags:

python

I'm about 2 weeks deep in my study of Python as an introductory language. I've hit a point in Zed's "Learn Python the Hard Way" where he suggests:

Use a while-loop only to loop forever, and that means probably never. This only applies to Python, other languages are different.

I've googled all over this, referenced everything I can, but I can't find any reason in the world why this would be a convention in Python. What makes it different?

When I gave up programming 10 years ago, I was working in VB and would regularly be told to get rid of my For loops and use While loops instead. I was a hack (as I am today, though I wrote a LOT of code back then), so I just did what I was told without questioning it. Well, now I'm questioning it. Is this a speed issue? Is it just to avoid escape-less infinites?

like image 847
Random_Person Avatar asked Nov 24 '10 18:11

Random_Person


People also ask

What's wrong with while loop?

Actually it is an infinite loop. while (r == false) ; is an infinite loop doing nothing, since r is initialized to false. Stop trying to put multiple things on a line. As much as anything, this is what caused your problem.

Do While loop should be avoided?

Avoiding while loops is one. His rule for for loops is that you use them when there is a list (or tuple or generator) of elements to iterate over or a fixed number of iterations. This is correct. When there is not, and you instead have a certain state or condition you want to reach, you use while loops.

Why should we avoid loops?

3: Statefulness: Contrary to initial perceptions, loops are all tied up in state. “Loops are really stateful. I would say loops are almost state infested” Emrich said. This is problematic when it comes to debugging software, given that the code you have at runtime isn't the same thing as what you see in your editor.


2 Answers

The advice seems poor to me. When you're iterating over some kind of collection, it is usually better to use one of Python's iteration tools, but that doesn't mean that while is always wrong. There are lots of cases where you're not iterating over any kind of collection.

For example:

def gcd(m, n):     "Return the greatest common divisor of m and n."     while n != 0:         m, n = n, m % n     return m 

You could change this to:

def gcd(m, n):     "Return the greatest common divisor of m and n."     while True:         if n == 0:             return m         m, n = n, m % n 

but is that really an improvement? I think not.

like image 87
Gareth Rees Avatar answered Oct 08 '22 20:10

Gareth Rees


It's because in the typical situation where you want to iterate, python can handle it for you. For example:

>>> mylist = [1,2,3,4,5,6,7] >>> for item in mylist: ...     print item ...  1 2 3 4 5 6 7 

Similar example with a dictionary:

>>> mydict = {1:'a', 2:'b', 3:'c', 4:'d'} >>> for key in mydict: ...     print "%d->%s" % (key, mydict[key]) ...  1->a 2->b 3->c 4->d 

Using a while loop just isn't necessary in a lot of common usage scenarios (and is not "pythonic").


Here's one comparison from a mailing list post that I though was a good illustration, too:

> 2. I know Perl is different, but there's just no equivalent of while
> ($line = <A_FILE>) { } ?

Python's 'for' loop has built-in knowledge about "iterable" objects, and that includes files. Try using:

for line in file:     ... 

which should do the trick.

like image 22
eldarerathis Avatar answered Oct 08 '22 20:10

eldarerathis