loops breaks and returns

Peter Hansen peter at engcorp.com
Sun Jan 1 17:13:16 EST 2006


rbt wrote:
> Is it more appropriate to do this:
> 
> while 1:
>      if x:
>          return x
> 
> Or this:
> 
> while 1:
>      if x:
>          break
> return x

The former would be considered bad style by some people.  Others would 
consider it perfectly acceptable in a small function (say, no more than 
10-20 lines of code?) where it would be clear what's going on.  Others 
would consider it fine in any case.

If I really had a "while 1" loop with only the one exit condition, and 
an immediate return, I would definitely go with the former approach.  If 
I had any other place where I was going to return, I'd consider the 
second approach more carefully.

-Peter




More information about the Python-list mailing list