[Python-ideas] Default return values to int and float

Mark Dickinson dickinsm at gmail.com
Mon Oct 3 14:06:48 CEST 2011


On Mon, Oct 3, 2011 at 12:07 PM, Dirkjan Ochtman <dirkjan at ochtman.nl> wrote:
> Converting two of the above examples:
>
> pv = float(ln['PV']) if ln['PV'] else None
> pv = float(ln['PV'], default=None)
>
> d.append(float(row[t]) if row[t] else 0.0)
> d.append(float(row[t], default=0.0))
>
> It's a little shorter and seems easier to read to me (less repetition).

But the two versions you give aren't equivalent.  With:

    pv = float(ln['PV']) if ln['PV'] else None

we'll get a ValueError if ln['PV'] contains some non-float, non-empty
garbage value.  With:

    pv = float(ln['PV'], default=None)

and (IIUC) the proposed semantics, that garbage value will be turned
into None instead, which
is definitely not what I'd want to happen in normal usage.

-- 
Mark



More information about the Python-ideas mailing list