Converting exponential numbers to strings.

Andrew Dalke dalke at dalkescientific.com
Tue Nov 6 12:25:12 EST 2001


Paul Rubin wrote:
>How about this:
>   try:
>      x = float(s)
>      result = 1
>   except:
>      result = 0

It's better to catch ValueError and not all exceptions.  Else
what would happen if you get a MemoryError or a KeyboardInterrupt?

try:
  float(s)
  result = 1
except ValueError:
  result = 0

In general, I consider 'except:' to be too blunt.  You should
catch only those exceptions you know about.  If you have to
catch everything, do

  except KeyboardInterrupt:
    raise
  except:
    ..

(If there's a callback, I often allow it to exit, so I often do
  except (KeyboardInterrupt, SystemExit):
    raise
  except:
    ..
)
                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list