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

David Townshend aquavitae69 at gmail.com
Mon Oct 3 09:52:12 CEST 2011


My idea is fairly simple: add a "default" argument to int and float,
allowing a return value if the conversion fails.  E.g:

>>> float('cannot convert this', default=0.0)
0.0

I think there are many use cases for this, every time float() or int()
are called with data that cannot be guaranteed to be numeric, it has
to be checked and some sort of default behaviour applied.  The above
example is just much cleaner than:

try:
    return float(s)
except ValueError:
    return 0.0


Any takers?

David



More information about the Python-ideas mailing list