what's this instance?

Discussion in 'Python' started by J. Peng, Jan 22, 2008.

  1. J. Peng

    J. Peng Guest

    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.
     
    J. Peng, Jan 22, 2008
    #1
    1. Advertising

  2. 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
     
    Marc 'BlackJack' Rintsch, Jan 22, 2008
    #2
    1. Advertising

  3. 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.
     
    Bruno Desthuilliers, Jan 22, 2008
    #3
  4. J. Peng

    J. Peng Guest

    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.:)
     
    J. Peng, Jan 22, 2008
    #4
  5. 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 ?-)
     
    Bruno Desthuilliers, Jan 22, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chris brat
    Replies:
    1
    Views:
    637
    chris brat
    May 10, 2006
  2. Sridhar R
    Replies:
    14
    Views:
    1,407
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  3. Kent Johnson
    Replies:
    4
    Views:
    408
    Terry Hancock
    Nov 13, 2004
  4. Gerry Sutton
    Replies:
    1
    Views:
    540
    Peter Otten
    Apr 16, 2005
  5. Gerard Flanagan
    Replies:
    3
    Views:
    447
    Terry Hancock
    Nov 19, 2005
Loading...

Share This Page