what's this instance?

J

J. Peng

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.
 
M

Marc 'BlackJack' Rintsch

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.

`oops` is bound to the `ValueError` or `TypError` object if `float()`
raises such an exception.

Ciao,
Marc 'BlackJack' Rintsch
 
B

Bruno Desthuilliers

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.
 
J

J. Peng

Bruno Desthuilliers дµÀ:
J. Peng a ¨¦crit :


For which definition of "works well" ?

I got it from Core Python Programming book I bought.You may ask it to
Westley Chun.:)
 
B

Bruno Desthuilliers

J. Peng a ¨¦crit :
Bruno Desthuilliers дµÀ:

I got it from Core Python Programming book I bought.You may ask it to
Westley Chun.:)

Ok: Mr Chun, if you here us ?-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top