On 17/04/2017 19:02, Ben Bacarisse wrote:
Marko Rauhamaa <ma...@pacujo.net> writes:

Terry Reedy <tjre...@udel.edu>:

On 4/17/2017 3:11 AM, Marko Rauhamaa wrote:
Here's statistics from a medium-sized project of mine:

   while True:            34
   while <condition>:     39
   for ... in ...:       158

As I posted previously, the ratio of for-loops in the stdlib is about 7
to 1.

What I notice in my numbers is that about one half of my while loops are
"while True", and about a third of my loops are while loops.

I fond the proportion on while True: loops surprising.  Is there
something about Python that encourages that kind of loop?


A few things:

(1) Python doesn't have the equivalent of C's comma operator, and no assignment in expressions. So if a condition relies on such set-up code, it can't do this:

   while (c=nextc())!=0:

But has to do this or use extra logic:

   while 1:
      c=nextc()
      if c==0: break

(2) There are exceptions ('raise') as an extra means of breaking out of such loops (as well as return, break, and exit()

(3) There's 'yield' too, as I've just seen this example:

    while True:
       yield []

So there are more excuses to make use of it. Plus Python programmers may be more averse to using convoluted logic just to avoid a 'break' in the middle of a loop.


--
bartc

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to