return in loop for ?

Roy Smith roy at
Thu Nov 24 16:17:45 CET 2005

"bonono at" <bonono at> wrote:
> 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

"Single entrance, single exit" is a philosophy (religion?) that's been 
around for a while.  The basic thought is that every code block should have 
a single entrance and a single exit.

Back in the dark old days of gotos, there was a lot of spaghetti code 
written with gotos jumping all over the place, even in and out of the 
middle of loops.  Then, in 1968, Dijkstra wrote his famous "Go To Statement 
Considered Harmful" ( which spawned the 
whole structured programming concept, and SESE is the logical outgrowth of 

The problem with SESE is that if you follow it strictly, you end up with 
things like the example given above where you have to invent some temporary 
variable, and an extra test at the end of the loop.  The cure is worse than 
the disease.

In any case, in a language which has exceptions, it's almost impossible to 
really have true SESE, since an exception could be thrown from almost 
anywhere.  To be fair, there are those who use this to argue that 
exceptions themselves are a bad thing.  In my last job, the official style 
guide said to not use exceptions in C++ because they generate confusing 
flow of control, but I think that's becomming the minority view these days.

More information about the Python-list mailing list