Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > what's this instance?

Reply
Thread Tools

what's this instance?

 
 
J. Peng
Guest
Posts: n/a
 
      01-22-2008
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.
 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      01-22-2008
On Tue, 22 Jan 2008 15:36:49 +0800, 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.


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

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      01-22-2008
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.
 
Reply With Quote
 
J. Peng
Guest
Posts: n/a
 
      01-22-2008
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" ?
>


I got it from Core Python Programming book I bought.You may ask it to
Westley Chun.
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      01-22-2008
J. Peng a ¨¦crit :
> 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" ?
>>

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


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57