return in loop for ?

bonono at gmail.com bonono at gmail.com
Thu Nov 24 01:06:37 EST 2005


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




More information about the Python-list mailing list