on the popularity of loops while and for

Terry Reedy tjreedy at udel.edu
Sat Aug 28 17:54:07 EDT 2021


On 8/28/2021 9:31 AM, Hope Rouselle wrote:
> I'd like get a statistic of how often each loop is used in practice.

My guess is that for loops are at least twice as common as while loops.

> I was trying to take a look at the Python's standard libraries --- those
> included in a standard installation of Python 3.9.6, say --- to see
> which loops are more often used among while and for loops.  Of course,
> since English use the preposition ``for'' a lot, that makes my life
> harder.  Removing comments is easy, but removing strings is harder.  So
> I don't know yet what I'll do.

Try something like

fors = 0
for file in files:
     for line in file:
         if re.match(r"/s*for .* in ", line):
             fors += 1

This excludes comprehensions, which have replaced some for loops.

-- 
Terry Jan Reedy



More information about the Python-list mailing list