try/except/else flexibility...

Ype Kingma ykingma at accessforall.nl
Sat Apr 13 18:07:33 EDT 2002


Magnus Lie Hetland wrote:
> 
> 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()
> 
In the pop() case there is a check that can be done before anything
needs to be done that might not be easily undone, ie. I suppose that

  try: somelist.pop()

is equivalent to:

  if somelist: somelist.pop()

In the general case:

  try: whatever()

a lot may have been done before an exception is evt. thrown
and I don't think there is a way to undo that in python without
a lot of programmer support in try/finally statements.
Databases have commit/rollback, but this requires expensive logging.


Regards,
Ype


P.S.
> Is this heresy?

If you look for a language that supports undoing assignments
try Sartre. Sartre supports unassignment, which might
come in handy. However "Unlike traditional programming languages
(or maybe very much like them), nothing in Sartre is guaranteed
except maybe for the fact that nothing is guaranteed",
so YMWV, ie. your milage will vary:
http://www.catseye.mb.ca/esoteric/sartre/index.html


-- 
email at xs4all.nl



More information about the Python-list mailing list