Pythonic way to determine if a string is a number

Steven D'Aprano steve at pearwood.info
Tue Feb 17 07:23:32 EST 2009


Roy Smith wrote:

>> > Do you really want to except SystemExit, KeyboardInterrupt, MemoryError
>> > and SyntaxError?
> 
> Absolutely.  Let's take my example -- you're writing software for a Mars
> Rover.  I have no idea how you might get a MemoryError, but let's say you
> do.  Which would you rather do, perform a system reset, or print a stack
> trace and wait for a friendly Martian to come along and reboot you?
> 
> You may think I'm being silly, but I'm dead serious.  The many layers of
> "It's impossible for this to happen, but if it does let's do something to
> try and recover" processing saved that mission several times over.  In
> some applications, there's no such thing as "halt".

Okay, but that surely falls under chapter 18 of the "Advanced Python
Programming for the Mars Rover" book rather than chapter 5 of "Newbies
Guide to Python".

In general, the right thing to do for unexpected exemptions is to let them
halt the program so you can find out about them and fix the bug, not to
hide the exception. There may be exceptions (pun intended) to this general
principle, but they're fairly unusual.


-- 
Steven




More information about the Python-list mailing list