extending the break statement

Michele Simionato mis6 at pitt.edu
Thu Oct 23 07:27:20 EDT 2003


tweedgeezer at hotmail.com (Jeremy Fincher) wrote in message news:<698f09f8.0310222043.3bbc351b at posting.google.com>...
> mis6 at pitt.edu (Michele Simionato) wrote in message news:<2259b0e2.0310220706.142bf8cb at posting.google.com>...
> > Is there a way of "ending" a module? I would like something like this:
> > 
> >   # mod.py
> >   print 'something here'
> >   end() # some mysterious function
> >   print 'you should not get here'
> > 
> >   # main.py
> >   import mod
> >   print 'it works'
> 
> # mod.py
> print 'something here'
> '''
> print 'you should not get here'
> '''
> 
> Just use triple-quote strings to "comment it out."
> 
> Jeremy

I am unhappy with this solution. Very often (and especially in the
case of experimental blocks of code) I use triple quoted docstrings
as comments, saying what the code should do and where is the problem,
so hopefully when I will go back to this code I will be able to fix
it. So my typical situation is


"""A block of working code"""

def working_function_with__limited_functionality:
    ...

###  <<=== I would like a good "break here!"

def experimental_function_which_will_be_better_once_finished:


"""This does not work because...."""


def helper_function1:
   ...


def helper_function2:

  ....


"""remember to refactor helper_function1,
improve helper_function2, etc. etc.
"""

As you see, not ideal for the triple quoted string idea. Besides, I 
don't like it. A ``break`` could be conditionally enabled according to
some condition and would be much nicer. Moreover, I don't see why 'break'
must be so limited; for instance, sometimes it would come handy to
exit from and if..elif.. block with a 'break'.
I don't think there would be any real difficulty in making 'break'
a general escaping construct, with the ability to escape at the end
of the current block of code (therefore ending the program when used
in the main script). Am I correct or I am missing some technicality?

One could argue that the use case is not so common, but it comes handy
sometime and it does not clutter at all the language (actually we
could avoid questions from newbies asking why 'break' does not
work in certain cases). I also remember somebody here 
advicing a newbie to use 'break' in an 'if' statement! This was
corrected, of course, but it is a trap one can easily fall in.


                              Michele




More information about the Python-list mailing list