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 something a certain number of times, and not about iterating
over an object.
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 general idea?
Also, what if you *do* need to just do something a set number of times.
Is this okay, or does it mean you are approaching the problem
incorrectly? Using for and range together seems to be a common idiom,
yet at the same time discouraged, so I''m wondering what is a good balance.
Thanks.
解决方案John Salerno 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 loops, or is it a
general idea?
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.
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 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 something a certain number of times, and not about iterating
over an object.
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 general idea?
Also, what if you *do* need to just do something a set number of times.
Is this okay, or does it mean you are approaching the problem
incorrectly? Using for and range together seems to be a common idiom,
yet at the same time discouraged, so I''m wondering what is a good balance.
Thanks.
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 general idea?
The use of range in for loops is not discouraged, just the unnecessary
use of it. In many languages, you were forced to use range - or size,
or length, or count, etc - because the only practical method of
iteration was to use successive indices into the array or other
collection, stopping when the indexes run out. Most loops operate on
some sort of structure in this way.
In Python, you can iterate directly over the structure instead, so you
no longer have to worry about keeping track of the current index,
comparing it to the maximum index, or collecting the right object from
the structure according to its index. Python just gives you the next
object until there are none left. As a result, for most looping tasks,
you have no need to use range or an equivalent.
Also, what if you *do* need to just do something a set number of times.
Is this okay, or does it mean you are approaching the problem
incorrectly?
Do you really need to do something "a number of times"? If so, range
(or xrange) is perfect for the job. More often though, you need to do
something "once for every ", so put your ''whatevers'' in a
list and iterate over that.
--
Ben Sizer