return in loop for ?

Steve Holden steve at holdenweb.com
Thu Nov 24 03:23:06 EST 2005


bonono at gmail.com wrote:
> Steve Holden wrote:
> 
> 
>>Yomgui: I am not a language lawyer, but I think you can feel safe
>>returning from inside a loop. Just as a matter of interest, how else
>>would you propose to implement the functionality Mike showed:
>>
>>
>>>>>>def f():
>>>
>>>...  for i in range(20):
>>>...   if i > 10: return i
>>>...
>>>
>>
>>Python is supposed to cleanly express the programmer's intent. I can;t
>>think of a cleaner way that Mike's - can you?
> 
> Interestingly, I just saw a thread over at TurboGears(or is it this
> group, I forgot) about this multiple return issue and there are people
> who religiously believe that a function can have only one exit point.
> 
> def f():
>   r = None
>   for i in range(20):
>   if i > 10:
>     r = 10
>     break
>   if r is None: something
>   else: return r
> 
Well, I'm happy in this instance that practicality beats purity, since 
the above is not only ugly in the extreme it's also far harder to read.

What are the claimed advantages for a single exit point? I'd have 
thought it was pretty obvious the eight-line version you gave is far 
more likely to contain errors than Mike's three-line version, wouldn't 
you agree?

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list