[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Does anyone else use this little idiom?

miller.paul.w

2/3/2008 2:04:00 AM

Ruby has a neat little convenience when writing loops where you don't
care about the loop index: you just do n.times do { ... some
code ... } where n is an integer representing how many times you want
to execute "some code."

In Python, the direct translation of this is a for loop. When the
index doesn't matter to me, I tend to write it as:

for _ in xrange (1,n):
some code

An alternative way of indicating that you don't care about the loop
index would be

for dummy in xrange (1,n):
some code

But I like using _ because it's only 1 character and communicates well
the idea "I don't care about this variable."

The only potential disadvantages I can see are threefold:

1. It might be a little jarring to people not used to it. I do admit
it looks pretty strange at first.

2. The variable _ has special meaning at the interactive interpreter
prompt. There may be some confusion because of this.

5. Five is right out. (ob Holy Grail reference, of course. :-)

So, I guess I'm wondering if anyone else uses a similar idiom and if
there are any downsides to it that I'm not aware of.

Thanks

Paul
30 Answers

Roy Smith

2/3/2008 2:35:00 AM

0

In article
<e82b13fe-0974-400f-ac5b-0583a86fd9e0@q39g2000hsf.googlegroups.com>,
miller.paul.w@gmail.com wrote:

> Ruby has a neat little convenience when writing loops where you don't
> care about the loop index: you just do n.times do { ... some
> code ... } where n is an integer representing how many times you want
> to execute "some code."
>
> In Python, the direct translation of this is a for loop. When the
> index doesn't matter to me, I tend to write it as:
>
> for _ in xrange (1,n):
> some code
>
> An alternative way of indicating that you don't care about the loop
> index would be
>
> for dummy in xrange (1,n):
> some code
>
> But I like using _ because it's only 1 character and communicates well
> the idea "I don't care about this variable."

Not to me. If I read "for _ in ...", I wouldn't be quite sure what _ was.
Is it some magic piece of syntax I've forgotten about? Or something new
added to language while I wasn't paying attention (I still consider most
stuff added since 1.5 to be new-fangled :-)). If I see "dummy", I know it
means, "the language requires a variable here, but the value is not needed".

> 1. It might be a little jarring to people not used to it. I do admit
> it looks pretty strange at first.
>
> 2. The variable _ has special meaning at the interactive interpreter
> prompt. There may be some confusion because of this.

Wow, I didn't even know about #2. Now you see what I mean about "some
magic syntax"? Surely, between, "It looks strange", and "there may be some
confusion", that's enough reason not to use it?

But, more to the point, I'd try to find variable name which described why I
was looping, even if I didn't actually use the value in the loop body:

for number_that_you_shall_count_to in xrange(3):
print "Whaaaaaaa"

Gabriel Genellina

2/3/2008 3:38:00 AM

0

En Sun, 03 Feb 2008 01:03:43 -0200, James Matthews <nytrokiss@gmail.com>
escribió:

Sorry to be nitpicking, but people coming from other languages may get
confused by the wrong examples:

> What i do is a simple range call. for i in range(number of times i want
> to repeat something)
> I guess it comes from my C days for(i=0;i<100;i++) { or in python for i
> in range(99):

Should be `for i in range(100)` to match exactly the C loop. Both iterate
100 times, with i varying from 0 to 99 inclusive.

>> miller.paul.w@gmail.com wrote:
>>
>> > Ruby has a neat little convenience when writing loops where you don't
>> > care about the loop index: you just do n.times do { ... some
>> > code ... } where n is an integer representing how many times you want
>> > to execute "some code."
>> >
>> > In Python, the direct translation of this is a for loop. When the
>> > index doesn't matter to me, I tend to write it as:
>> >
>> > for _ in xrange (1,n):
>> > some code

Should be `for _ in xrange(n)` to match the Ruby example. Both iterate n
times.

> On Feb 3, 2008 3:34 AM, Roy Smith <roy@panix.com> wrote:
>
>> But, more to the point, I'd try to find variable name which described
>> why I was looping, even if I didn't actually use the value in theloop
>> body:

Me too. Government don't collect taxes by the number of variable names
used (yet).

--
Gabriel Genellina

Jeff Schwab

2/3/2008 3:48:00 AM

0

How miller.paul.w@gmail.com wrote:
> Ruby has a neat little convenience when writing loops where you don't
> care about the loop index: you just do n.times do { ... some
> code ... } where n is an integer representing how many times you want
> to execute "some code."
>
> In Python, the direct translation of this is a for loop. When the
> index doesn't matter to me, I tend to write it as:
>
> for _ in xrange (1,n):
> some code
>
> An alternative way of indicating that you don't care about the loop
> index would be
>
> for dummy in xrange (1,n):
> some code
>
> But I like using _ because it's only 1 character and communicates well
> the idea "I don't care about this variable."
>
> The only potential disadvantages I can see are threefold:
>
> 1. It might be a little jarring to people not used to it. I do admit
> it looks pretty strange at first.
>
> 2. The variable _ has special meaning at the interactive interpreter
> prompt. There may be some confusion because of this.
>
> 5. Five is right out. (ob Holy Grail reference, of course. :-)
>
> So, I guess I'm wondering if anyone else uses a similar idiom and if
> there are any downsides to it that I'm not aw

Would something like this be acceptable? It still requires a loop
variable, plus an extra line of code per loop, plus a one-time class
definition (and import into each client module), and it's probably
slower than "for dummy in range." The syntax might be more inuitive
than "dummy" or "_" in a for loop, though.

class Go:
def __init__(self, count):
self.count = count

def again(self):
if self.count <= 0:
return False
self.count -= 1
return True

go = Go(3)
while go.again():
print "hello"

Ben Finney

2/3/2008 4:09:00 AM

0

miller.paul.w@gmail.com writes:

> When the index doesn't matter to me, I tend to write it as:
>
> for _ in xrange (1,n):
> some code
>
> An alternative way of indicating that you don't care about the loop
> index would be
>
> for dummy in xrange (1,n):
> some code
>
> But I like using _ because it's only 1 character and communicates well
> the idea "I don't care about this variable."

Not to me. As you noted, '_' is easily ambiguous. Explicit is better
than implicit; the name 'dummy' makes it much clearer.

> The only potential disadvantages I can see are threefold:
>
> 1. It might be a little jarring to people not used to it. I do admit
> it looks pretty strange at first.
>
> 2. The variable _ has special meaning at the interactive interpreter
> prompt. There may be some confusion because of this.

The name '_' also has an established meaning for the "gettext"
internationalisation/localisation library, pre-dating Python and
documented in <URL:http://www.python.org/doc/lib/module-g....

--
\ "I have a large seashell collection, which I keep scattered on |
`\ the beaches all over the world. Maybe you've seen it." |
_o__) â??Steven Wright |
Ben Finney

Ben Finney

2/3/2008 4:13:00 AM

0

"Gabriel Genellina" <gagsl-py2@yahoo.com.ar> writes:

> Should be `for _ in xrange(n)` to match the Ruby example. Both
> iterate n times.

Only until Python 3.0, since the 'xrange' implementation will become
'range' at that time.

<URL:http://wiki.python.org/moin/Py3kDeprecated#head-343618ffa0887790ed12c3f9cf278cd7ca7e...

--
\ "Prediction is very difficult, especially of the future." |
`\ â??Niels Bohr |
_o__) |
Ben Finney

Steven D'Aprano

2/3/2008 5:50:00 AM

0

On Sat, 02 Feb 2008 18:03:54 -0800, miller.paul.w wrote:

> for _ in xrange (1,n):
> some code
....
> So, I guess I'm wondering if anyone else uses a similar idiom and if
> there are any downsides to it that I'm not aware of.

Sometimes, but not often.

If I'm writing a use-once-then-throw-away script, it's too much trouble
to press the Shift key to get an underscore *wink*, so I tend to just use
i or x for the loop variable.

If I'm writing something I intend to keep, but don't care too much about,
I'll use _, i or x about equal numbers of times, depending on whim.

If I was writing something important I expected others to read, I'd use
dummy.

I wouldn't go so far as to say "Don't use that idiom!", but it's not
something I use often.


--
Steven

Steven D'Aprano

2/3/2008 6:29:00 AM

0

On Sun, 03 Feb 2008 15:08:34 +1100, Ben Finney wrote:

>> But I like using _ because it's only 1 character and communicates well
>> the idea "I don't care about this variable."
>
> Not to me. As you noted, '_' is easily ambiguous. Explicit is better
> than implicit; the name 'dummy' makes it much clearer.

Assuming you're not writing code like the following:

dummies = [Pacifier() for x in xrange(100)]
for dummy in dummies:
check_for_quality(dummy)


"dummy" as the name of a variable you don't care about is just a
convention, no different from "_" or "paris_hilton" or "shrubbery". In
fact, to me "dummy" implies that it holds a dummy value that will be
replaced later with the actual value needed.

If you want an explicit name, try a variation of "dontcare". Assuming
that you're an English speaker.

People seem to forget that "explicit" just means that there's a
convention that nearly everybody knows, and if you follow it, nearly
everybody will know what you mean. Often that convention is nothing more
than the meanings of words in whatever human language you're speaking,
but it's still a convention.



--
Steven

Arnaud Delobelle

2/3/2008 9:21:00 AM

0

On Feb 3, 2:03 am, miller.pau...@gmail.com wrote:
> Ruby has a neat little convenience when writing loops where you don't
> care about the loop index: you just do n.times do { ... some
> code ... } where n is an integer representing how many times you want
> to execute "some code."
>
> In Python, the direct translation of this is a for loop.  When the
> index doesn't matter to me, I tend to write it as:
>
> for _ in xrange (1,n):
>    some code
[...]

If 'some code' is a function (say f) you can write (with repeat from
itertools):

for action in repeat(f, n): action()

I don't know how 'Pythonic' this would be...

--
Arnaud

Robert Kern

2/3/2008 10:56:00 AM

0

James Matthews wrote:
> Because 0 is counted therefore i only have to do it 99 times

No, Gabriel is correct. range(n) creates a list of integers starting at 0 and
going to n-1 (inclusive), not n.


In [1]: range(9)
Out[1]: [0, 1, 2, 3, 4, 5, 6, 7, 8]

In [2]: len(range(9))
Out[2]: 9

In [3]: len(range(1, 9))
Out[3]: 8

In [4]: range(10)
Out[4]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [5]: len(range(10))
Out[5]: 10


> On Feb 3, 2008 4:38 AM, Gabriel Genellina <gagsl-py2@yahoo.com.ar
> <mailto:gagsl-py2@yahoo.com.ar>> wrote:
>
> En Sun, 03 Feb 2008 01:03:43 -0200, James Matthews
> <nytrokiss@gmail.com <mailto:nytrokiss@gmail.com>>
> escribió:
>
> Sorry to be nitpicking, but people coming from other languages may get
> confused by the wrong examples:
>
> > What i do is a simple range call. for i in range(number of times
> i want
> > to repeat something)
> > I guess it comes from my C days for(i=0;i<100;i++) { or in python
> for i
> > in range(99):
>
> Should be `for i in range(100)` to match exactly the C loop. Both
> iterate
> 100 times, with i varying from 0 to 99 inclusive.
>
> >> miller.paul.w@gmail.com <mailto:miller.paul.w@gmail.com> wrote:
> >>
> >> > Ruby has a neat little convenience when writing loops where
> you don't
> >> > care about the loop index: you just do n.times do { ... some
> >> > code ... } where n is an integer representing how many times
> you want
> >> > to execute "some code."
> >> >
> >> > In Python, the direct translation of this is a for loop. When the
> >> > index doesn't matter to me, I tend to write it as:
> >> >
> >> > for _ in xrange (1,n):
> >> > some code
>
> Should be `for _ in xrange(n)` to match the Ruby example. Both iterate n
> times.
>
> > On Feb 3, 2008 3:34 AM, Roy Smith <roy@panix.com
> <mailto:roy@panix.com>> wrote:
> >
> >> But, more to the point, I'd try to find variable name which
> described
> >> why I was looping, even if I didn't actually use the value in
> theloop
> >> body:
>
> Me too. Government don't collect taxes by the number of variable names
> used (yet).
>
> --
> Gabriel Genellina

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Steve Holden

2/3/2008 12:16:00 PM

0

Gabriel Genellina wrote:
[...]
>> On Feb 3, 2008 3:34 AM, Roy Smith <roy@panix.com> wrote:
>>
>>> But, more to the point, I'd try to find variable name which described
>>> why I was looping, even if I didn't actually use the value in theloop
>>> body:
>
> Me too. Government don't collect taxes by the number of variable names
> used (yet).
>
And if they did I'd pay the tax to retain my sanity.

no-taxation-without-repr()-ly y'rs - steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.hold...