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

  2. `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. Advertisements

  3. J. Peng a ¨¦crit :
    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.
    cf other answers on this.
     
    Bruno Desthuilliers, Jan 22, 2008
    #3
  4. J. Peng

    J. Peng Guest

    Bruno Desthuilliers дµÀ:
    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 :
    Ok: Mr Chun, if you here us ?-)
     
    Bruno Desthuilliers, Jan 22, 2008
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.