python for loop

andrew cooke andrew at acooke.org
Wed Apr 1 07:04:12 EDT 2009


something i don't think has been mentioned much - if you're using
"range()" in your python code then you're almost always doing it wrong.

i just grepped lepl and i use range 20 times in 9600 lines of code.  out
of those, all but 3 are in "quick and dirty" tests or experimental code,
not in the main library itself (which is 6300 lines).

so in my main code, i use range() once in every 2000 lines of code,
approximately.

the three examples are:

(1) where i need to access two adjacent members of a list, and which has a
comment in the code explaining why it is not an error (in other words, i
was so unhappy with my code i needed to leave a note explaining why it was
like that)

(2) a use irrelevant to this discussion because i do not use the value to
an index an array.

(3) in the rather complex implementation of a circular buffer.

so in a small/moderate size library of 600 lines (including blanks and
comments, but excluding tests and exploratory code) the only time i have
used range with array indices i was either unhappy with the code, or
implementing a complex data structure.

andrew





More information about the Python-list mailing list