Re: Does anyone else use this little idiom?

2008-02-05 Thread Nick Craig-Wood
[EMAIL PROTECTED] [EMAIL PROTECTED] 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

Re: Does anyone else use this little idiom?

2008-02-05 Thread Eduardo O. Padoan
On Feb 5, 2008 1:30 PM, Nick Craig-Wood [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] [EMAIL PROTECTED] 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

Re: Does anyone else use this little idiom?

2008-02-04 Thread BJörn Lindqvist
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 I usually

Re: Does anyone else use this little idiom?

2008-02-04 Thread Steven D'Aprano
On Mon, 04 Feb 2008 15:08:44 +, Bob Martin wrote: Rexx's method is the way to do it : do 50 I tried writing Rexx code and executing it in Python, but I got unexpected results, mostly SyntaxError exceptions. Is that a bug in Python? No-I'm-not-really-serious-ly yours, -- Steven --

Re: Does anyone else use this little idiom?

2008-02-04 Thread Bob Martin
in 332496 20080204 102153 =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?= [EMAIL PROTECTED] wrote: 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

Re: Does anyone else use this little idiom?

2008-02-03 Thread Arnaud Delobelle
On Feb 3, 2:03 am, [EMAIL PROTECTED] 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

Re: Does anyone else use this little idiom?

2008-02-03 Thread James Matthews
Because 0 is counted therefore i only have to do it 99 times Thanks On Feb 3, 2008 4:38 AM, Gabriel Genellina [EMAIL PROTECTED] wrote: En Sun, 03 Feb 2008 01:03:43 -0200, James Matthews [EMAIL PROTECTED] escribió: Sorry to be nitpicking, but people coming from other languages may get

Re: Does anyone else use this little idiom?

2008-02-03 Thread Robert Kern
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]:

Re: Does anyone else use this little idiom?

2008-02-03 Thread Steve Holden
Gabriel Genellina wrote: [...] On Feb 3, 2008 3:34 AM, Roy Smith [EMAIL PROTECTED] 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

Re: Does anyone else use this little idiom?

2008-02-03 Thread Steve Holden
Jeff Schwab wrote: How [EMAIL PROTECTED] 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

Re: Does anyone else use this little idiom?

2008-02-03 Thread Stef Mientki
be careful, _ is thé translation function used in Il8N, Il10N localization / internationalization e.g. print _( hello ) cheers, Stef [EMAIL PROTECTED] 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

Re: Does anyone else use this little idiom?

2008-02-03 Thread Ivan Illarionov
Plain Python function are very often more powerful than classes: def go(count): ... if not hasattr(go, 'count'): ... go.count = count ... if go.count = 0: ... del go.count ... return False ... go.count -= 1 ... return True ... while go(3): ... print 'hello' ... hello hello

Re: Does anyone else use this little idiom?

2008-02-03 Thread Zentrader
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

Re: Does anyone else use this little idiom?

2008-02-03 Thread Paul McGuire
On Feb 2, 9:48 pm, Jeff Schwab [EMAIL PROTECTED] wrote: How [EMAIL PROTECTED] 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

Re: Does anyone else use this little idiom?

2008-02-03 Thread miller . paul . w
On Feb 3, 10:42 am, Zentrader [EMAIL PROTECTED] wrote: 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

Re: Does anyone else use this little idiom?

2008-02-03 Thread miller . paul . w
On Feb 3, 11:20 am, Paul McGuire [EMAIL PROTECTED] wrote: [... some code... some words ... more code, etc. ...] But this still seems like a lot of work to avoid for x in range(n):. I agree. The point of me using for _ in xrange (n) isn't to avoid the for loop at all. What I wanted was a

Re: Does anyone else use this little idiom?

2008-02-03 Thread miller . paul . w
My apologies if any attributions are messed up. On Feb 3, 1:28 am, Steven D'Aprano [EMAIL PROTECTED] cybersource.com.au wrote: 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

Re: Does anyone else use this little idiom?

2008-02-03 Thread Troels Thomsen
for action in repeat(f, n): action() I don't know how 'Pythonic' this would be... agree, or this: import itertools def f1(): print hello [f() for f in itertools.repeat(f1,6)] tpt -- http://mail.python.org/mailman/listinfo/python-list

Re: Does anyone else use this little idiom?

2008-02-03 Thread Thomas Dybdahl Ahle
On Sat, 2008-02-02 at 18:03 -0800, [EMAIL PROTECTED] wrote: for _ in xrange (1,n): some code I'd always use i for loop variables I don't know what to call. It stands for iterator or something. In a nested loop the next variable would simply be called j and so on. I also tend to use _, but

Re: Does anyone else use this little idiom?

2008-02-03 Thread Marc 'BlackJack' Rintsch
On Sun, 03 Feb 2008 15:13:14 +1100, Ben Finney wrote: Gabriel Genellina [EMAIL PROTECTED] 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. The point wasn't

Re: Does anyone else use this little idiom?

2008-02-03 Thread Marc 'BlackJack' Rintsch
On Sun, 03 Feb 2008 05:33:16 -0800, Ivan Illarionov wrote: Plain Python function are very often more powerful than classes: def go(count): ... if not hasattr(go, 'count'): ... go.count = count ... if go.count = 0: ... del go.count ... return False ... go.count -= 1

Re: Does anyone else use this little idiom?

2008-02-03 Thread Paul Rubin
[EMAIL PROTECTED] writes: where n is an integer representing how many times you want to execute some code. ... I tend to write it as: for _ in xrange (1,n): some code But this does it n-1 times, not n times. -- http://mail.python.org/mailman/listinfo/python-list

Re: Does anyone else use this little idiom?

2008-02-03 Thread Ivan Illarionov
On Feb 4, 12:56 am, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: On Sun, 03 Feb 2008 05:33:16 -0800, Ivan Illarionov wrote: Plain Python function are very often more powerful than classes: def go(count): ... if not hasattr(go, 'count'): ... go.count = count ... if go.count

Re: Does anyone else use this little idiom?

2008-02-03 Thread samwyse
[EMAIL PROTECTED] wrote: My apologies if any attributions are messed up. On Feb 3, 1:28 am, Steven D'Aprano [EMAIL PROTECTED] cybersource.com.au wrote: If you want an explicit name, try a variation of dontcare. Assuming that you're an English speaker. I'm with Steven here. I typically use

Does anyone else use this little idiom?

2008-02-02 Thread miller . paul . w
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

Re: Does anyone else use this little idiom?

2008-02-02 Thread Roy Smith
In article [EMAIL PROTECTED], [EMAIL PROTECTED] 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,

Re: Does anyone else use this little idiom?

2008-02-02 Thread James Matthews
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;i100;i++) { or in python for i in range(99): On Feb 3, 2008 3:34 AM, Roy Smith [EMAIL PROTECTED] wrote: In article [EMAIL PROTECTED], [EMAIL PROTECTED] wrote:

Re: Does anyone else use this little idiom?

2008-02-02 Thread Gabriel Genellina
En Sun, 03 Feb 2008 01:03:43 -0200, James Matthews [EMAIL PROTECTED] 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

Re: Does anyone else use this little idiom?

2008-02-02 Thread Jeff Schwab
How [EMAIL PROTECTED] 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

Re: Does anyone else use this little idiom?

2008-02-02 Thread Ben Finney
[EMAIL PROTECTED] 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

Re: Does anyone else use this little idiom?

2008-02-02 Thread Ben Finney
Gabriel Genellina [EMAIL PROTECTED] 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.

Re: Does anyone else use this little idiom?

2008-02-02 Thread Steven D'Aprano
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

Re: Does anyone else use this little idiom?

2008-02-02 Thread Steven D'Aprano
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.