a good explanation

S

s99999999s2003

hi
my friend has written a loop like this
cnt = 0
files = [a,b,c,d]
while cnt < len(files) :
do_something(files[cnt])

i told him using
for fi in files:
do_something(fi)

is better, because the while loop method makes another call to
len..which is slower..
am i partly right? or is there a better explanation for using the for
method.?
thanks
 
B

Bas

I guess that your version is faster, although the difference would be
negligible in this small example. The for loop is probably optimized
for speed under the hood (it is written in C), while the 'while' loop
is performed in python, which is much slower.

Much more important than the speed difference is the clarity: your
version is the accepted practice in the python world, so people will
understand it immediately. It also saves two lines of code. And most of
all, it prevents you from making mistakes: your friend, for example,
has forgotten to increase cnt, so he created an infinite loop!

Bas
 
D

Diez B. Roggisch

hi
my friend has written a loop like this
cnt = 0
files = [a,b,c,d]
while cnt < len(files) :
do_something(files[cnt])

i told him using
for fi in files:
do_something(fi)

is better, because the while loop method makes another call to
len..which is slower..
am i partly right? or is there a better explanation for using the for
method.?

Several good reasons:

- his loop won't terminate, as he (or you trying to copy his efforts)
forgot to increment cnt

- he needs additional statements & state. The more you have of this,
the likelier you make errors. he could for example write <= len(files)

- the whole loop is noisier to the eye, makes it harder to grasp what
it's all about

It seems that your friend comes from a language like C or JAVA (pre 1.5)
where the for loop was basically what his while loop above is: one
initializer, one condition test, one last statement, mostly incrementing
the counter.

Python's for is build around the concept of an iterable. Which lists,
tuples, strings and many other things are - and thus the last, and
possibly strongest reason is: its not idiomatic Python, he tries top
shoehorn python into some schemes he's used from other languages. Don't
do that, accept Python & it's ways for a better life :)


Diez
 
J

John Machin

hi
my friend has written a loop like this
cnt = 0
files = [a,b,c,d]
while cnt < len(files) :
do_something(files[cnt])

i told him using
for fi in files:
do_something(fi)

is better, because the while loop method makes another call to
len..which is slower..
am i partly right? or is there a better explanation for using the for
method.?


You are partially right.
More reasons:
(1) It's more elegant; you don't have the "cnt" if you don't need it,
there's no extraneous evaluation of len(files).
(2) You don't get the chance to omit cnt += 1, like your friend did
(ROTFLMAO).
Cheers,
John
 
M

mik3

wow!...thanks for both replies..and yes you are right, he comes from a
world of C and java...i have successfully "brainwashed" him to try out
coding in python...haha.
So he has written his first program in python and i have roughly toook
a glance at it and saw what he did.. i pointed out his "mistakes" but
couldn't convince him otherwise. Anyway , i am going to show him your
replies..
thanks again.
 
N

Nick Craig-Wood

mik3 said:
So he has written his first program in python and i have roughly toook
a glance at it and saw what he did.. i pointed out his "mistakes" but
couldn't convince him otherwise. Anyway , i am going to show him your
replies..

You might want to show him this too...if you happen to need cnt in the
loop, this is what you write

files = [a,b,c,d]
for cnt, fi in enumerate(files):
do_something(cnt, fi)
 
R

rainbow.cougar

Nick said:
mik3 said:
So he has written his first program in python and i have roughly toook
a glance at it and saw what he did.. i pointed out his "mistakes" but
couldn't convince him otherwise. Anyway , i am going to show him your
replies..

You might want to show him this too...if you happen to need cnt in the
loop, this is what you write

files = [a,b,c,d]
for cnt, fi in enumerate(files):
do_something(cnt, fi)

Like many things in Python, measurement is the key, Being an old C
coder. I quite often write:

i=0
while i<end_condition:
something=stuff
i++

and try to be a good pythonesita translate it to:
for something in stuff:
#stuff code

A few weeks ago I found myself manipulating a list of lists that I
wanted to use as columns and discovered that in:

def resort(list,d2list):
newlist=[]
"""
while (i < d2len):
newlist.append(list[d2list[1]])
i+=1
"""
for row in d2list:
newlist.append(list[row[1]])


return newlist


ran as fast with the while as the for. However, for 'elegance', and to
stick to python style, I kept the for.

Something that is being missed is the idea of changing conditions. A
for loop assumes known boundaries.

def condition_test():
# check socket status
# return true if socket good, false otherwise

while condition_test():
# do stuff

allows the loopiing code to react to changing conditions. Which of
couse is why we like to keep while loops around ;-0

Curtis
 
D

Diez B. Roggisch

Something that is being missed is the idea of changing conditions. A
for loop assumes known boundaries.

def condition_test():
# check socket status
# return true if socket good, false otherwise

while condition_test():
# do stuff

allows the loopiing code to react to changing conditions. Which of
couse is why we like to keep while loops around ;-0

You can always use break to, well, break out of the loop - and as for is
working over iterables which might very well be generators that deliver
an infinite amount of data, the break-condition can (and often will,
even if one used while) work on the current object instead of some
otherwise unsused index.

So above becomes:


for item in items:
if condition(item):
break
...

where the while would be still pretty ugly IMHO :)


Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top