Re: Is using range() in for loops really Pythonic?
- From: Ivan Illarionov <ivan.illarionov@xxxxxxxxx>
- Date: Mon, 12 May 2008 02:33:41 +0000 (UTC)
On Sun, 11 May 2008 18:52:48 -0700, Carl Banks wrote:
On May 11, 6:44 pm, Ben Finney <bignose+hates-s...@xxxxxxxxxxxxxxx>
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
.
- References:
- Is using range() in for loops really Pythonic?
- From: John Salerno
- Re: Is using range() in for loops really Pythonic?
- From: XLiIV
- Re: Is using range() in for loops really Pythonic?
- From: John Salerno
- Re: Is using range() in for loops really Pythonic?
- From: Ben Finney
- Re: Is using range() in for loops really Pythonic?
- From: Carl Banks
- Is using range() in for loops really Pythonic?
- Prev by Date: anonymous assignment
- Next by Date: Re: anonymous assignment
- Previous by thread: Re: Is using range() in for loops really Pythonic?
- Next by thread: Convention for name indicating "don't care about the value" (was: Is using range() in for loops really Pythonic?)
- Index(es):
Relevant Pages
|