PEP 284, Integer for-loops

Peter Hansen peter at engcorp.com
Thu Mar 7 08:15:57 EST 2002


Jonathan Hogg wrote:
> 
> On 6/3/2002 16:41, in article
> eppstein-BB3186.08413106032002 at news.service.uci.edu, "David Eppstein"
> <eppstein at ics.uci.edu> wrote:
> 
> >   One of the most common uses of for-loops in Python is to iterate
> >   over an interval of integers. [...]
> 
> Is it that common?
> 
> I did a quick grep over the 2.2 library and found that around 10% of for
> loops used range. Of course, the library is pretty unrepresentative code
> since, by design, it contains all the stuff you *wouldn't* want to do
> yourself.
> 
> I did the same grep over the random Python code in my projects directory and
> found that 3% of my for loops use range. This was 12 ranges out of 408 loops
> in about 18,000 lines of code.
> 
> 12 times in the last 18,000 lines of code I've had to loop over a range.
> 
> Am I unusual in saying that I really don't care?

I don't think so, at least among programmers approaching Python
programming in a Pythonic fashion. :-)

I think I rarely use for with a range any more, because I tend to
think "if I need to do something eight times, there are probably 
eight things I'm doing something to so I should just take those
things and iterate over a list of them".  And it's rare to have
to prepare a list of exactly eight things (which would need the
for/range since you don't have the "things" already).

I don't care either, and in fact I care so little that I am against
the PEP because it adds unnecessary clutter to special cases etc.
as mentioned by others.

-Peter



More information about the Python-list mailing list