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
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
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
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.
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
|
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?
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
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
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
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?
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
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
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
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,
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
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
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
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
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
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
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)
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
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
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
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,
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
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
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
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
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
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
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
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
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
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
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
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
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
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:
---
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
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
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
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::
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
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
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
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
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
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
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
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
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
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
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
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
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
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?
--
[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
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
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
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
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
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
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
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
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
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
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
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
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
70 matches
Mail list logo