Is using range() in for loops really Pythonic?

Ivan Illarionov ivan.illarionov at gmail.com
Sun May 11 22:33:41 EDT 2008


On Sun, 11 May 2008 18:52:48 -0700, Carl Banks wrote:

> On May 11, 6:44 pm, Ben Finney <bignose+hates-s... at benfinney.id.au>
> wrote:
>> In such cases, the name 'dummy' is conventionally bound to the items
>> from the iterator, for clarity of purpose::
>>
>>     for dummy in range(10):
>>         # do stuff that makes no reference to 'dummy'
> 
> Is this documented?  I've never heard of this convention.  It's not PEP
> 8, and I've never seen consistent usage of any name.  I'd be interested
> in knowing where you read that this was a convention, or in what
> subcommunities it's a convention in.
> 
> I think dummy is a terrible name to use for this, since in no other
> usage I can think of does the word "dummy" suggest something isn't used.
>  In fact, quite the opposite.  For example, the dummy_threads module is
> most definitely used; the word dummy means that it's stripped down. 
> Based on that, your usage of the symbol dummy above would suggest to me
> that it's a value used in lieu of something else (such as a calculated
> value).
> 
> In mathematics, a dummy argument another name for the independent
> variable of a function (or more accurately, the symbol used to represent
> it), which also doesn't match your usage.
> 
> If a value isn't used, then I think the most clear name for it is
> "unused".
> 
> 
> Carl Banks

I agree with Carl. This group is the only place I've heard about this 
convension and it looks terrible IMO. Even if a value is not used, the 
variable still has a meaning: it is a counter, or an index.

I know about the other convention: "for i in xrange" or "for i in range".
Wikipedia agrees with me:
http://en.wikipedia.org/wiki/For_loop
Examples in wikipedia article use either "i" or "counter".

It is established convention in almost every programming language and its 
origins are in mathematics (matrix indices).

Google search for "for dummy in range" (with quotes) gives 164 results
While "for dummy in range" gives 146 000 results. Almost thousand times 
more.

So, "i" is more conventional and as such is more readable.

-- Ivan



More information about the Python-list mailing list