473,786 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does anyone else use this little idiom?

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
Feb 3 '08 #1
18 1459
In article
<e8************ *************** *******@q39g200 0hsf.googlegrou ps.com>,
mi***********@g mail.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"
Feb 3 '08 #2
En Sun, 03 Feb 2008 01:03:43 -0200, James Matthews <ny*******@gmai l.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.
> mi***********@g mail.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 <ro*@panix.comw rote:
>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

Feb 3 '08 #3
"Gabriel Genellina" <ga*******@yaho o.com.arwrites:
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-343618ffa088779 0ed12c3f9cf278c d7ca7eef51-2>

--
\ "Prediction is very difficult, especially of the future." |
`\ —Niels Bohr |
_o__) |
Ben Finney
Feb 3 '08 #4
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
Feb 3 '08 #5
On Feb 3, 2:03*am, miller.pau...@g mail.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

Feb 3 '08 #6
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 <ga*******@yaho o.com.ar
<mailto:ga***** **@yahoo.com.ar >wrote:

En Sun, 03 Feb 2008 01:03:43 -0200, James Matthews
<ny*******@gmai l.com <mailto:ny***** **@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.
> mi***********@g mail.com <mailto:mi***** ******@gmail.co mwrote:
>>
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 <ro*@panix.co m
<mailto:ro*@pan ix.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

Feb 3 '08 #7
Gabriel Genellina wrote:
[...]
>On Feb 3, 2008 3:34 AM, Roy Smith <ro*@panix.comw rote:
>>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.holdenweb.com/

Feb 3 '08 #8
be careful, "_" is thé translation function used in Il8N, Il10N
localization / internationaliz ation
e.g.
print _( "hello" )

cheers,
Stef

mi***********@g mail.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 aware of.

Thanks

Paul
Feb 3 '08 #9
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 :-)).
+1 to forgotten about
+1 to new-fangled=added since 1.5 When 3000 comes out I'll have to
break down and buy a new Python book. Also, it's amazing how much
posting space is spent here replying to someone who is too lazy to key
in a variable name. Damn!
Feb 3 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
2513
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const keyword a lot less usable. Can anybody tell me what that good reason is? TIA,
12
1647
by: Oliver Knoll | last post by:
Ok, I've searched this group for Big/Little endian issues, don't kill me, I know endianess issues have been discussed a 1000 times. But my question is a bit different: I've seen the follwing function several times, it converts data stored in Big Endian (BE) format into host native format (LE on LE machines, BE on BE machines):
8
2195
by: Dgates | last post by:
Has anyone typed up an index for the O'Reilly book "C# and VB.NET Conversion?" I'm just learning C#, and often using this little book to see which VB.NET terms translate directly to some term in C#. However, it's a real hassle that the book has no index, just a table of contents. For example, as early as page 8, the book teaches that C#'s "using" statement is the equivalent of VB.NET's "imports" statement. However, that concept...
5
1566
by: Grant Olson | last post by:
I'm feeling a little guilty here. I spent a lot of my free time last year working on an x86 compiler for python. I made a reasonable amount of progress, but my interests wandered off into other areas. I basically just got bored and stopped working on the thing maybe 6 months ago. So today, I went to the directory that contains all of my source checkouts, and saw this v 0.1 compiler just sitting out there AGAIN. It's not that I don't...
17
3079
by: christophe.chazeau | last post by:
Hi, I have a problem with a really simple chunk of code which should work but does obviously does not. This chunk of code is just a POC aimed at finding a bug in a larger project in which the same problem seems to occur. Here the deal : when I run this piece of code, I expect all the memory allocated by the "Test" object to be freed but what I observe is that after the second sleep (after all the additions to the vector), the memory...
55
6249
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
13
2285
by: Anonymous | last post by:
On MS site: http://msdn2.microsoft.com/en-us/library/esew7y1w(VS.80).aspx is the following garbled rambling: "You can avoid exporting classes by defining a DLL that defines a class with virtual functions, and functions you can call to instantiate and delete objects of the type. You can then just call virtual functions on the type."
8
4090
by: Andrew Savige | last post by:
I'm learning Python by reading David Beazley's "Python Essential Reference" book and writing a few toy programs. To get a feel for hashes and sorting, I set myself this little problem today (not homework, BTW): Given a string containing a space-separated list of names: names = "freddy fred bill jock kevin andrew kevin kevin jock" produce a frequency table of names, sorted descending by frequency. then ascending byname. For...
5
3817
by: jbperez808 | last post by:
I find myself having to do the following: x = (some complex expression) y = x if x else "blah" and I was wondering if there is any built-in idiom that can remove the need to put (some complex expression) in the temporary variable x. e.g. something like the below:
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10360
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.