loops breaks and returns

Sam Pointon free.condiments at gmail.com
Sun Jan 1 18:10:09 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
>
> Or, does it matter?

I would pick the first form if that's the only place where x would be
returned from the function. However, if there would be redundant
'return x'-es in the function because of this, then the second form
becomes preferable. It's not set in stone, though - if you have a
compelling reason to break the rule-of-thumb, do so.




More information about the Python-list mailing list