PEP 284, Integer for-loops

Jonathan Hogg jonathan at onegoodidea.com
Thu Mar 7 04:26:04 EST 2002


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?

>   The perceived lack of a natural, intuitive integer iteration
>   syntax has led to heated debate on python-list, and spawned at
>   least four PEPs before this one. [...]

I keep getting the feeling that the "perceived lack" is from people who's
perception is skewed by other languages. To a new programmer, teaching them
that 'for' iterates over sequences and that 'range' makes sequences of
integers is pretty easy (and yes, I used to teach). The problem only seems
to be with programmers in other languages lamenting the lack of the BASIC,
C, or whatever loop structures.

If you're already a programmer then it shouldn't be that hard for you to
adapt. If it is, then you were badly taught ;-)

Jonathan




More information about the Python-list mailing list