Python tricks

Paul Rubin http
Mon Jan 12 09:02:52 EST 2009


RajNewbie <raj.indian.08 at gmail.com> writes:
>    I do understand that we can use the code like -
>    i = 0
>    while True:
>      i++
>      if i > 200: raise infinite_Loop_Exception
>      ...
>      if <condition>: break
> 
>    But I am not very happy with this code for 3 reasons

I prefer:

   from itertools import count

   for i in count():
       if i > 200: raise infinite_Loop_Exception
       ...

You could also use:

   for i in xrange(200): 
      ...
   else: 
      raise infinite_Loop_Exception

The "else" clause runs only if no break statement is executed.



More information about the Python-list mailing list