Newbie getting desperate with for

Michael Torrie torriem at gmail.com
Fri Feb 18 17:55:09 CET 2011


On 02/17/2011 09:15 PM, Larry Hudson wrote:
> A true time waster indeed -- it's an infinite loop that will _never_ end.
> 
> Others have already about the need of the shebang line to run as a python script, but I'm 
> surprised no one mentioned how truly useless this code is.
> 
> The i = 0 line is totally unnecessary.  The variable i is created and set to zero by the first 
> iteration of the for loop.  The break will abort the for loop (NOT the while loop) in the first 
> iteration, and the 2nd through the 10th iterations will be skipped altogether.

To be fair the range() call will be fairly time and memory-consuming on
python 2, since it returns a list.

> 
> This effectively leaves your code as:
> 
> while True:
>      pass      #  Do nothing, forever
> 
> An empty loop as a time delay can occasionally be useful, but as a practical matter, a for loop 
> with only 10 (empty/pass) iterations is probably too short for anything useful.  Instead of 
> being empty, do some more complex (but ignored) operation -- say math.sqrt() or math.sin() for 
> example -- and a much larger repetition count.  But it's likely that does use up processor 
> cycles unnecessarily, although it can give you delays of fractions of seconds.  If you want 
> delays greater than a second, check out the time.sleep() function.
> 
>       -=- Larry -=-




More information about the Python-list mailing list