break LABEL vs. exceptions + PROPOSAL

Evan Simpson evan at tokenexchange.com
Thu Sep 2 12:52:30 EDT 1999


Here's a (shamelessly unmotivated) example which I find more expressive than
it would be with "break LABEL":

Found, Panic = 'Found', 'Panic'
try:
   for thingy in whatzits:
      for bit in thingy:
         if bit.is_wanted():
            raise Found, bit
         if hasattr(bit, 'panic'):
            raise Panic, bit.panic
      if thingy.other_bit.is_wanted()
         raise Found, thingy.other_bit
      if hasattr(thingy, 'panic'):
         raise Panic, thingy.panic
   else:
      print "Not found"
except Found, it:
   print "Found", it
except Panic, why
   print "Panic!", why

In particular, "break LABEL" doesn't allow you to provide information about
*why* you're leaving the loop, and *what* caused that condition without
explicitly setting up variables to hold that information and then checking
the reason, post-loop.  Exceptions are more general, too; there doesn't have
to be a loop.

The only things I don't like about this are the unnecessary involvement of
the fairly heavy exception machinery, the need to "declare" my break reasons
as variables to ensure an "is" match, and the possibility of a subtle typo
turning my local "break" into an uncaught exception.  Rather than "break
LABEL", I would like to see a construction like the following: (in 2.0, of
course <wink>)

try:
  ...
  break <string>, <value>
  ...
continue <string1>, <name-binding-list1>:
  <suite1>
continue <string2>, <name-binding-list2>:
  <suite>

The first argument to "break" and "continue" is required to be a string, and
each "break" string is required to match a string in a containing try's
"continue".  This makes it clear that we're breaking out of the suite rather
than raising an exception, and could be implemented efficiently and
trivially with jump, load, and store bytecodes.

Rich Salz <salzr at certco.com> wrote in message
news:rst6om9lj7141 at corp.supernews.com...
> >No, I'm sorry, the expressivity of "break LABEL" and "continue LABEL"
> >are much clearer than the cicumlocutions necessary for exceptions.
>
> You sure?  I wasn't aware that I could break and return a value, such as
> the name of the file that wasn't found.  At least not without a lot of
> circuml^H^H^H^H^H global variables.







More information about the Python-list mailing list