try/except/else flexibility...

Magnus Lie Hetland mlh at vier.idi.ntnu.no
Fri Apr 12 00:30:27 EDT 2002


Just a thought: Given the prevalence of try/except/else in the "don't
look before you leap" way of doing things, could it be useful to make
the statement more flexible? For instance, instead of writing

  try: foo + ''
  except: pass
  else: bar()

(assuming I'm sloppy enough not to specify TypeError on the second
line) one could simply write:

  try: foo + ''
  else: bar()

and if I want something done only if it causes no exceptions, for
instance pop off an element of a list unless it is empty, I could just
do:

  try: somelist.pop()

Is this heresy? I see the harm in not specifying exactly which
exceptions you're expecting, of course -- are there other arguments
against this? (I'm sure that might be argument enough... Oh, well...)

--
Magnus Lie Hetland                                  The Anygui Project
http://hetland.org                                  http://anygui.org



More information about the Python-list mailing list