GoTo considered missing
Denis S. Otkidach
ods at fep.ru
Wed Jul 3 06:25:33 EDT 2002
On Tue, 2 Jul 2002, Terry Hancock wrote:
TH> > Breaks and Continues are fine for single loops, but:
TH> > What is the approved Python method for getting from the
TH> bottom of a set of
TH> > nested loops to the top?
[...]
TH> You should probably try to use an exception in this
TH> case.
TH>
TH> class ImDone(Exception):
TH> # ... I don't remember this bit because I remember
TH> # the now-deprecated exceptions-are-strings syntax
TH> pass
TH>
TH> try:
TH> loop ...
TH> loop ...
TH> loop ...
TH> loop ...
TH> if
TH> special-reason-I-need-to-bail-out-like-Im-finished-early:
TH> raise ImDone
TH> except ImDone:
TH> # stuff I need to do after finishing
I'd prefer to tell explicitly what loop to break/continue,
something like this:
loop...:
loop...:
...
if ...:
: break
elif ...:
: continue
...
(place your favourite character insted ":")
where indention is used twice to describe current block and what
block to return to on break/continue.
P.S. Such nested loops aren't so widely used to worry about.
More information about the Python-list
mailing list