PEP 284, Integer for-loops

Tim Legant tim-dated-1016292456.1fd5be at catseye.net
Sat Mar 9 10:27:33 EST 2002


usenet at microtonal.co.uk (Graham Breed) writes:

> Tim Legant wrote:
> 
> > Out of curiosity I ran a couple of quick greps over the TMDA source.
> > TMDA doesn't really fall into either of your "alternative" categories;
> > it's small, but definitely not throwaway and it's an application, not
> > a core library.  It's a mail processor to eliminate SPAM.  However,
> > it's pure Python; there's no integration with C or Java.
> 
> How small is small?

I did a quick wc -l.  We have 8 scripts calling upon 9 modules (in a
package).  The total line count is just over 4500, but since the usage
docstring in each of the 8 scripts is fairly long, I'm going to guess
that the real line count is closer to 3300.

> I have a script at <http://www.microtonal.co.uk/temper.py> with
> around 1490 lines (including comments and blank lines) and 35 uses
> of range, some probably evil, most for for loops.  Two warty
> examples
> 
>   for n in range(1,n+1,2):
> 
> and
> 
>       for element in range(firstIndex, maxBasis+1):
> 
> where the +1 is used for inclusive boundaries.  7 straight 
> range(len(..))s.

I think there's no question that range is sometimes needed, and that
in certain cases it reads a little wonky!  In your latter 7 cases I'd
probably loop over the sequence and keep track of the index myself,
but I don't think there's any clear reason for preferring one way over
the other.

> Well, I've stuck my head above the parapet.  Have a look at the code, and 
> see if I'm expressing myself badly.  It doesn't integrate with C, Java or 
> COM but does do a lot of work with integers.  That's not something Python 
> excels at, but it's such a good language generally that it's still worth 
> using outside its niche.

This is also a good point.  Since TMDA processes text (email messages
and filtering rules) we don't use integers much.  I would imagine
that, when your problem domain is numbers, you'd have to loop over
them a great deal more.  I think this is what David Eppstein has been
discovering in his class, as well.


Tim




More information about the Python-list mailing list