Is inifinite loop not a good practice?

Roy Smith roy at panix.com
Mon Feb 20 17:01:06 EST 2006


In article <mailman.2182.1140471719.27775.python-list at python.org>,
 "Terry Reedy" <tjreedy at udel.edu> wrote:

> "Alvin A. Delagon" <adelagon at gmail.com> wrote in message 
> news:43F9DF68.5020301 at gmail.com...
> > I've been hearing comments that infinite loop is a bad programming
> > practice.
> 
> What is bad is an *unintended* loop that goobles cpu time while outputting 
> nothing.  Example:
> 
> def fact(n):
>   res = 1
>   while n != 0:
>     res *= n
>     n -= 1
>   return res
> 
> fact(-1)
> 
> Now imagine that you are paying $360/hr ($.10/sec) (for IBM mainframe time) 
> and you should understand the bad reputation (and why jobs were submitted 
> with a time limit!). ;-)
> 
> Terry Jan Reedy

Could be worse.  You could have written:

def fact(n):
   if n == 0:
      return 1
   else:
     return n * fact (n-1)

then you would have not only gotten zonked on CPU charges, but on memory 
charges as well :-)



More information about the Python-list mailing list