a good explanation
Diez B. Roggisch
deets at nospam.web.de
Fri May 26 10:58:56 EDT 2006
> 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
More information about the Python-list
mailing list