Is using range() in for loops really Pythonic?

Grant Edwards grante at visi.com
Mon May 12 10:34:48 EDT 2008


On 2008-05-12, Ben Finney <bignose+hates-spam at benfinney.id.au> wrote:

>> Maybe my brain works differently, but I find both "dummy" and
>> "unused" are extremely confusing names for loop counters. The loop
>> begins to look like it doesn't iterate at all if its counter is
>> dummy or unused.
>> 
>> If it *counts* it is *used* and it's *not* dummy.
>
> The value is unused by any of the code inside the block. For the
> purposes of that block, it is a dummy value.

The value may be unused, but for me it's the name that matters,
not the value.   The name might be in use by other code, and
the careless choice of a "dummy" name that's _supposed_ to be
unused has broken code precisely becuase the name was being
used (for something else).  Requiring that the user pollute a
namespace with a useless name is a wart.

> That is also regrettably common in Python code. It still
> suffers from being unnecessarily ambiguous, since there are
> *also* plenty of loops using 'i', 'j', etc. where the loop
> counter *is* used.

Perhaps I'm the only one who's ever been stupid enough to
overwrite an index named "i" (that is being used) with another
index named "i" (that isn't being used)...


-- 
Grant Edwards                   grante             Yow! All of life is a blur
                                  at               of Republicans and meat!
                               visi.com            



More information about the Python-list mailing list