Re: Is using range() in for loops really Pythonic?

2008-05-19 Thread Dave Parker
Your point about for-loops was applicable not only to Python, but to many other programming languages. So in response, I've added two new for-loop variations to Flaming Thunder. The two new variations are for-forever-do and for-expression-times-do. For-forever allows you to explicitly create

Re: Is using range() in for loops really Pythonic?

2008-05-16 Thread Graham Breed
George Sakkis wrote: If you push this logic too far, you should del every name immediately after the last statement it is used in the scope. I would generally find less readable some code spread with del every few lines, micro- managing the effective scope of each name. YMMV. Yes, but ... how

Re: Is using range() in for loops really Pythonic?

2008-05-14 Thread Marc 'BlackJack' Rintsch
On Tue, 13 May 2008 10:20:41 -0700, John Nagle wrote: Matt Nordhoff wrote: Well, you should use xrange(10) instead of range(10). CPython really is naive. That sort of thing should be a compile-time optimization. It's not naive, it can't know at compile time what object is bound to

Re: Is using range() in for loops really Pythonic?

2008-05-14 Thread cokofreedom
On May 14, 8:37 am, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: On Tue, 13 May 2008 10:20:41 -0700, John Nagle wrote: Matt Nordhoff wrote: Well, you should use xrange(10) instead of range(10). CPython really is naive. That sort of thing should be a compile-time optimization.

Re: Is using range() in for loops really Pythonic?

2008-05-13 Thread Terry Reedy
John Salerno [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] | The reason I even brought this up is because I remember someone saying a | while back (probably here on the newsgroup) that the true use of a for | loop was to iterate through a sequence (for the purpose of using that |

Re: Is using range() in for loops really Pythonic?

2008-05-13 Thread Marc 'BlackJack' Rintsch
On Mon, 12 May 2008 11:23:07 -0700, Carl Banks wrote: On May 12, 3:42 am, Ben Finney [EMAIL PROTECTED] wrote: Because of the precedent of those names, choosing one of those names doesn't make it clear to the reader that the value is never used; Why is it even necessary to document this?

Re: Is using range() in for loops really Pythonic?

2008-05-13 Thread Dave Parker
REXX's loop construct subsumes all the common uses... And worse, it appears that a repetition and a condition can be part of the single statement. Thank you for pointing out the REXX examples. I am a Kedit user, but had forgotten about the REXX do-loops. I'll keep them in mind when I

Re: Is using range() in for loops really Pythonic?

2008-05-13 Thread Larry Bates
John Salerno wrote: I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is unPythonic. The reason I ask is because the structure of the for loop seems to be for iterating through

Re: Is using range() in for loops really Pythonic?

2008-05-13 Thread Lie
On May 13, 11:01 am, John Salerno [EMAIL PROTECTED] wrote: Ben Finney wrote: I think that the idiom     for unused in xrange(10):         # do stuff with no reference to 'unused' is quite common. Is that what you're asking about? Yes. I was more or less asking about the specific

Re: Is using range() in for loops really Pythonic?

2008-05-13 Thread Lie
On May 13, 9:20 pm, Lie [EMAIL PROTECTED] wrote: On May 13, 11:01 am, John Salerno [EMAIL PROTECTED] wrote: Ben Finney wrote: I think that the idiom     for unused in xrange(10):         # do stuff with no reference to 'unused' is quite common. Is that what you're asking about?

Re: Is using range() in for loops really Pythonic?

2008-05-13 Thread John Nagle
Matt Nordhoff wrote: John Salerno wrote: I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is unPythonic. The reason I ask is because the structure of the for loop seems to be

Re: Is using range() in for loops really Pythonic?

2008-05-13 Thread Ben Finney
John Nagle [EMAIL PROTECTED] writes: Matt Nordhoff wrote: Well, you should use xrange(10) instead of range(10). CPython really is naive. That sort of thing should be a compile-time optimization. Or even a case of the 'xrange' behaviour making 'range' obsolete. Which, as I pointed out

Re: Is using range() in for loops really Pythonic?

2008-05-12 Thread Ben Finney
Ivan Illarionov [EMAIL PROTECTED] writes: In such cases, the name 'dummy' is conventionally bound to the items from the iterator, for clarity of purpose [..] If a value isn't used, then I think the most clear name for it is unused. [...] Maybe my brain works differently, but I find

Re: Is using range() in for loops really Pythonic?

2008-05-12 Thread Paddy
On May 11, 9:28 pm, Arnaud Delobelle [EMAIL PROTECTED] wrote: Hi John, Arnaud; Contrived example: # Print 'hello' 10 times; x is not used for x in xrange(10): print 'hello' I've used Fortran and C and so would tend to use either i,j,k as the unused loop variable above, or, for clarity,

Re: Is using range() in for loops really Pythonic?

2008-05-12 Thread Ivan Illarionov
On Mon, 12 May 2008 16:24:23 +1000, Ben Finney wrote: [...] That is also regrettably common in Python code. It still suffers from being unnecessarily ambiguous, since there are *also* plenty of loops using 'i', 'j', etc. where the loop counter *is* used. Differentiating these use cases by

Re: Is using range() in for loops really Pythonic?

2008-05-12 Thread Roel Schroeven
John Salerno schreef: Ben Finney wrote: John Salerno [EMAIL PROTECTED] writes: num = 33 for x in xrange(10): print num += 1 Which is better done by 'num += 10'. Can you come up with an example that isn't trivially replaced with clearer code? That might make it clearer what your

Re: Is using range() in for loops really Pythonic?

2008-05-12 Thread Ben Finney
Ivan Illarionov [EMAIL PROTECTED] writes: On Mon, 12 May 2008 16:24:23 +1000, Ben Finney wrote: [...] That is also regrettably common in Python code. It still suffers from being unnecessarily ambiguous, since there are *also* plenty of loops using 'i', 'j', etc. where the loop counter

Re: Is using range() in for loops really Pythonic?

2008-05-12 Thread Ben Finney
Paddy [EMAIL PROTECTED] writes: I've used Fortran and C and so would tend to use either i,j,k as the unused loop variable above, or, for clarity, call it something descriptive like loop_count, if the loop body would be clearer. The problem with all of these names is that they also have long

Re: Is using range() in for loops really Pythonic?

2008-05-12 Thread Ivan Illarionov
On Mon, 12 May 2008 16:24:23 +1000, Ben Finney wrote: [...] That is also regrettably common in Python code. It still suffers from being unnecessarily ambiguous, since there are *also* plenty of loops using 'i', 'j', etc. where the loop counter *is* used. Differentiating these use cases

Re: Is using range() in for loops really Pythonic?

2008-05-12 Thread Grant Edwards
On 2008-05-12, Ben Finney [EMAIL PROTECTED] wrote: Maybe my brain works differently, but I find both dummy and unused are extremely confusing names for loop counters. The loop begins to look like it doesn't iterate at all if its counter is dummy or unused. If it *counts* it is *used* and

Re: Is using range() in for loops really Pythonic?

2008-05-12 Thread Grant Edwards
On 2008-05-12, Ben Finney [EMAIL PROTECTED] wrote: I too, agree that requiring a name be bound to the values coming out of the iterator seems wrong. With do something N times, there must be *something* to keep track of which iteration we're up to (or, equivalently, how many iterations remain)

Re: Is using range() in for loops really Pythonic?

2008-05-12 Thread Grant Edwards
On 2008-05-12, Ben Finney [EMAIL PROTECTED] wrote: Paddy [EMAIL PROTECTED] writes: I've used Fortran and C and so would tend to use either i,j,k as the unused loop variable above, or, for clarity, call it something descriptive like loop_count, if the loop body would be clearer. The problem

Re: Is using range() in for loops really Pythonic?

2008-05-12 Thread Paddy
On May 12, 3:46 pm, Grant Edwards [EMAIL PROTECTED] wrote: On 2008-05-12, Ben Finney [EMAIL PROTECTED] wrote: Paddy [EMAIL PROTECTED] writes: I've used Fortran and C and so would tend to use either i,j,k as the unused loop variable above, or, for clarity, call it something descriptive

Re: Is using range() in for loops really Pythonic?

2008-05-12 Thread Carl Banks
On May 12, 3:42 am, Ben Finney [EMAIL PROTECTED] wrote: Because of the precedent of those names, choosing one of those names doesn't make it clear to the reader that the value is never used; Why is it even necessary to document this? This is the thing that baffles me the most about this

Re: Is using range() in for loops really Pythonic?

2008-05-12 Thread Ben Finney
Carl Banks [EMAIL PROTECTED] writes: IMHO, whether a varibale is used or not has got to be one of the least important things of all (in no small part because it's easily discernable from nearby code). I couldn't disagree more. If you're binding a name to a value that will never be used,

Re: Is using range() in for loops really Pythonic?

2008-05-12 Thread Carl Banks
On May 12, 7:03 pm, Ben Finney [EMAIL PROTECTED] wrote: Carl Banks [EMAIL PROTECTED] writes: IMHO, whether a varibale is used or not has got to be one of the least important things of all (in no small part because it's easily discernable from nearby code). I couldn't disagree more. If

Re: Is using range() in for loops really Pythonic?

2008-05-12 Thread Terry Reedy
Ben Finney [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] | So, when not using the values that come from the controlling iterator, | it's good to make that explicit. If Python supported it, we might | prefer to use no name at all for something that isn't used, but the | 'for' syntax

Re: Is using range() in for loops really Pythonic?

2008-05-12 Thread Dave Parker
On May 10, 8:19 pm, John Salerno [EMAIL PROTECTED] wrote: It seems somewhat artificial to use the for loop to do something a certain number of times, like above. I agree; it's a common flaw with lots of languages, not just Python. I'd be inclined to use something like: FOR 8 TIMES DO

Re: Is using range() in for loops really Pythonic?

2008-05-12 Thread George Sakkis
On May 12, 7:03 pm, Ben Finney [EMAIL PROTECTED] wrote: Carl Banks [EMAIL PROTECTED] writes: IMHO, whether a varibale is used or not has got to be one of the least important things of all (in no small part because it's easily discernable from nearby code). I couldn't disagree more. If

Re: Is using range() in for loops really Pythonic?

2008-05-12 Thread John Salerno
Ben Finney wrote: I think that the idiom for unused in xrange(10): # do stuff with no reference to 'unused' is quite common. Is that what you're asking about? Yes. I was more or less asking about the specific situation of using a for loop to do something X number of times, but

Re: Is using range() in for loops really Pythonic?

2008-05-11 Thread XLiIV
On May 11, 4:19 am, John Salerno [EMAIL PROTECTED] wrote: I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10):     #do something 10 times is unPythonic. The reason I ask is because the structure of the

Re: Is using range() in for loops really Pythonic?

2008-05-11 Thread castironpi
On May 11, 12:38 am, Paul Rubin http://[EMAIL PROTECTED] wrote: John Salerno [EMAIL PROTECTED] writes: for x in range(10):     #do something 10 times is unPythonic. The reason I ask is because the structure of the for loop seems to be for iterating through a sequence. It seems somewhat

Re: Is using range() in for loops really Pythonic?

2008-05-11 Thread Terry Reedy
XLiIV [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] On May 11, 4:19 am, John Salerno [EMAIL PROTECTED] wrote: I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is

Re: Is using range() in for loops really Pythonic?

2008-05-11 Thread John Salerno
John Salerno wrote: I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is unPythonic. The reason I ask is because the structure of the for loop seems to be for iterating through

Re: Is using range() in for loops really Pythonic?

2008-05-11 Thread John Salerno
XLiIV wrote: The range() function returns a list and list is a sequence, isn't? I think you're missing the point. To me, there seems to be a fundamental difference between these two things: --- people = ['Sam', 'Bob', 'Fred'] for name in people: print name --- AND --- num = 33

Re: Is using range() in for loops really Pythonic?

2008-05-11 Thread Arnaud Delobelle
John Salerno [EMAIL PROTECTED] writes: John Salerno wrote: I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is unPythonic. The reason I ask is because the structure of the

Re: Is using range() in for loops really Pythonic?

2008-05-11 Thread Ben Finney
John Salerno [EMAIL PROTECTED] writes: num = 33 for x in xrange(10): print num += 1 Which is better done by 'num += 10'. Can you come up with an example that isn't trivially replaced with clearer code? That might make it clearer what your concern is. The [above] example [...] is

Re: Is using range() in for loops really Pythonic?

2008-05-11 Thread Yves Dorfsman
John Salerno wrote: To me, the first example is a pure use of the for loop. You are iterating through an object and *using* the items you are stepping through. The second example, however, is simply doing something 10 times, and what it's doing has nothing to do with 'x' or xrange. So it

Re: Is using range() in for loops really Pythonic?

2008-05-11 Thread Jonathsn Cronin
On Sun, 11 May 2008 16:16:53 -0400, John Salerno wrote (in message [EMAIL PROTECTED]): XLiIV wrote: The range() function returns a list and list is a sequence, isn't? I think you're missing the point. To me, there seems to be a fundamental difference between these two things: ---

Re: Is using range() in for loops really Pythonic?

2008-05-11 Thread Carl Banks
On May 11, 6:44 pm, Ben Finney [EMAIL PROTECTED] 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

Re: Is using range() in for loops really Pythonic?

2008-05-11 Thread Ivan Illarionov
On Sun, 11 May 2008 18:52:48 -0700, Carl Banks wrote: On May 11, 6:44 pm, Ben Finney [EMAIL PROTECTED] 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

Convention for name indicating don't care about the value (was: Is using range() in for loops really Pythonic?)

2008-05-11 Thread Ben Finney
Carl Banks [EMAIL PROTECTED] writes: On May 11, 6:44 pm, Ben Finney [EMAIL PROTECTED] 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

Re: Is using range() in for loops really Pythonic?

2008-05-11 Thread John Salerno
Ben Finney wrote: John Salerno [EMAIL PROTECTED] writes: num = 33 for x in xrange(10): print num += 1 Which is better done by 'num += 10'. Can you come up with an example that isn't trivially replaced with clearer code? That might make it clearer what your concern is. ::sigh::

Re: Convention for name indicating don't care about the value (was: Is using range() in for loops really Pythonic?)

2008-05-11 Thread Carl Banks
On May 11, 11:41 pm, Ben Finney [EMAIL PROTECTED] wrote: Carl Banks [EMAIL PROTECTED] writes: On May 11, 6:44 pm, Ben Finney [EMAIL PROTECTED] wrote: In such cases, the name 'dummy' is conventionally bound to the items from the iterator, for clarity of purpose:: for dummy in

Re: Is using range() in for loops really Pythonic?

2008-05-11 Thread Ivan Illarionov
In such cases, the name 'dummy' is conventionally bound to the items from the iterator, for clarity of purpose [..] If a value isn't used, then I think the most clear name for it is unused. [...] Maybe my brain works differently, but I find both dummy and unused are extremely confusing names

Re: Is using range() in for loops really Pythonic?

2008-05-11 Thread Ben Finney
John Salerno [EMAIL PROTECTED] writes: Ben Finney wrote: John Salerno [EMAIL PROTECTED] writes: num = 33 for x in xrange(10): print num += 1 Which is better done by 'num += 10'. Can you come up with an example that isn't trivially replaced with clearer code? That

Is using range() in for loops really Pythonic?

2008-05-10 Thread John Salerno
I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is unPythonic. The reason I ask is because the structure of the for loop seems to be for iterating through a sequence. It seems

Re: Is using range() in for loops really Pythonic?

2008-05-10 Thread Matt Nordhoff
John Salerno wrote: I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is unPythonic. The reason I ask is because the structure of the for loop seems to be for iterating

Re: Is using range() in for loops really Pythonic?

2008-05-10 Thread Daniel Fetchinson
I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is unPythonic. The reason I ask is because the structure of the for loop seems to be for iterating through a sequence. It

Re: Is using range() in for loops really Pythonic?

2008-05-10 Thread 7stud
On May 10, 8:19 pm, John Salerno [EMAIL PROTECTED] wrote: I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10):     #do something 10 times is unPythonic. The reason I ask is because the structure of the

Re: Is using range() in for loops really Pythonic?

2008-05-10 Thread Paddy
On May 11, 3:19 am, John Salerno [EMAIL PROTECTED] wrote: I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is unPythonic. The reason I ask is because the structure of the

Re: Is using range() in for loops really Pythonic?

2008-05-10 Thread Paul Rubin
John Salerno [EMAIL PROTECTED] writes: for x in range(10): #do something 10 times is unPythonic. The reason I ask is because the structure of the for loop seems to be for iterating through a sequence. It seems somewhat artificial to use the for loop to do something a certain number of

Re: using range() in for loops

2006-04-05 Thread Ben Sizer
John Salerno wrote: The reason for this distinction comes from the fact that I read a lot how using range and for is somewhat discouraged, because it doesn't really use a for loop for it's true purpose. So my question is, is this just a Python-oriented opinion about for loops, or is it a

Re: using range() in for loops

2006-04-05 Thread Ant
It's not just a Python thing, Java for example generally uses the idiom: for (Iterator it = list.iterator(); it.hasNext(); ) { Object next = it.next(); //Do stuff to next } Horrible compared to the python idiom of course (though the latest version supports for (x : list){}) Ruby has

Re: using range() in for loops

2006-04-05 Thread Roel Schroeven
John Salerno schreef: I'm reading Text Processing in Python right now and I came across a comment that is helping me to see for loops in a new light. I think because I'm used to the C-style for loop where you create a counter within the loop declaration, for loops have always seemed to me

Re: using range() in for loops

2006-04-05 Thread John Salerno
Roel Schroeven wrote: In many cases loops really are for iterating over sequences; more so than I realized when using for loops in C or C++. In these cases, Python's for statement works better than C-style loops. And if you really need to do something a certain number of times, there's

Re: using range() in for loops

2006-04-05 Thread AndyL
Paul Rubin wrote: Normally you'd use range or xrange. range builds a complete list in memory so can be expensive if the number is large. xrange just counts up to that number. so when range would be used instead of xrange. if xrange is more efficient, why range was not reimplemented? --

Re: using range() in for loops

2006-04-05 Thread Georg Brandl
[EMAIL PROTECTED] wrote: hi John, Python doesn't provide for loop like C / C++ but using Range() or Xrange() you can achive all the functionalities of the C for loop. Not quite. Georg -- http://mail.python.org/mailman/listinfo/python-list

Re: using range() in for loops

2006-04-05 Thread Georg Brandl
was not reimplemented? Because of backwards compatibility. range() returns a list, xrange() an iterator: list(xrange(...)) will give the same results as range(...). In for loops, using xrange instead of range makes no difference since the loop only iterates over the range. But it's a problem when someone just

Re: using range() in for loops

2006-04-05 Thread Adam DePrince
On Tue, 2006-04-04 at 21:54 -0400, John Salerno wrote: I'm reading Text Processing in Python right now and I came across a comment that is helping me to see for loops in a new light. I think because I'm used to the C-style for loop where you create a counter within the loop declaration, for

Re: using range() in for loops

2006-04-05 Thread Sion Arrowsmith
AndyL [EMAIL PROTECTED] wrote: Paul Rubin wrote: Normally you'd use range or xrange. range builds a complete list in memory so can be expensive if the number is large. xrange just counts up to that number. so when range would be used instead of xrange. if xrange is more efficient, why range

Re: using range() in for loops

2006-04-05 Thread Steven D'Aprano
On Wed, 05 Apr 2006 09:16:37 -0400, AndyL wrote: Paul Rubin wrote: Normally you'd use range or xrange. range builds a complete list in memory so can be expensive if the number is large. xrange just counts up to that number. so when range would be used instead of xrange. if xrange is more

Re: using range() in for loops

2006-04-05 Thread Steven D'Aprano
On Wed, 05 Apr 2006 16:15:12 +0200, Georg Brandl wrote: [EMAIL PROTECTED] wrote: hi John, Python doesn't provide for loop like C / C++ but using Range() or Xrange() you can achive all the functionalities of the C for loop. Not quite. Care to explain what the differences are, or shall

Re: using range() in for loops

2006-04-05 Thread Steven D'Aprano
On Wed, 05 Apr 2006 16:21:02 +0200, Georg Brandl wrote: Because of backwards compatibility. range() returns a list, xrange() an iterator: list(xrange(...)) will give the same results as range(...). Georg is pretty much correct in his explanation, but just to dot all the I's and cross all the

Re: using range() in for loops

2006-04-05 Thread Georg Brandl
Steven D'Aprano wrote: On Wed, 05 Apr 2006 16:21:02 +0200, Georg Brandl wrote: Because of backwards compatibility. range() returns a list, xrange() an iterator: list(xrange(...)) will give the same results as range(...). Georg is pretty much correct in his explanation, but just to dot all

Re: using range() in for loops

2006-04-05 Thread Georg Brandl
Steven D'Aprano wrote: On Wed, 05 Apr 2006 16:15:12 +0200, Georg Brandl wrote: [EMAIL PROTECTED] wrote: hi John, Python doesn't provide for loop like C / C++ but using Range() or Xrange() you can achive all the functionalities of the C for loop. Not quite. Care to explain what

Re: using range() in for loops

2006-04-05 Thread Alex Martelli
Georg Brandl [EMAIL PROTECTED] wrote: Steven D'Aprano wrote: On Wed, 05 Apr 2006 16:15:12 +0200, Georg Brandl wrote: [EMAIL PROTECTED] wrote: hi John, Python doesn't provide for loop like C / C++ but using Range() or Xrange() you can achive all the functionalities of the C for

using range() in for loops

2006-04-04 Thread John Salerno
I'm reading Text Processing in Python right now and I came across a comment that is helping me to see for loops in a new light. I think because I'm used to the C-style for loop where you create a counter within the loop declaration, for loops have always seemed to me to be about doing

Re: using range() in for loops

2006-04-04 Thread Paul Rubin
John Salerno [EMAIL PROTECTED] writes: The reason for this distinction comes from the fact that I read a lot how using range and for is somewhat discouraged, because it doesn't really use a for loop for it's true purpose. So my question is, is this just a Python-oriented opinion about for

Re: using range() in for loops

2006-04-04 Thread sushant . sirsikar
hi John, Python doesn't provide for loop like C / C++ but using Range() or Xrange() you can achive all the functionalities of the C for loop.If you wants distributed for loop You can use Xrange. John Salerno wrote: I'm reading Text Processing in Python right now and I came across a comment