what's this instance?

Robert Kern robert.kern at gmail.com
Tue Jan 22 03:09:39 EST 2008


J. Peng wrote:
> def safe_float(object):
>   try:
>     retval = float(object)
>   except (ValueError, TypeError), oops:
>     retval = str(oops)
>   return retval
> 
> x=safe_float([1,2,3,4])
> print x
> 
> 
> The code above works well.But what's the instance of "oops"? where is it
> coming from? I'm totally confused on it.thanks.

The line

   except (ValueError, TypeError), oops:

will trap ValueError and TypeError exceptions. The actual exception object will 
be assigned to the name "oops".

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list