J. Peng a ¨¦crit :
def safe_float(object):
try:
retval = float(object)
except (ValueError, TypeError), oops:
retval = str(oops)
return retval
The code above works well.
For which definition of "works well" ?
This function is really ill-named - it returns either a float or a
string, so it is definitively not safe :
def dosomething(x):
return x + (x / 0.5)
x=safe_float([1,2,3,4])
// a dozen line of code here
y = dosomething(x)
And now, have fun trying to trace the real problem... Better to not use
this function at all IMHO - at least, you'll get a meaningfull traceback.
But what's the instance of "oops"? where is it
coming from? I'm totally confused on it.thanks.
cf other answers on this.