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

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Oct 3 12:40:04 CEST 2011


David Townshend wrote:
> 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)

I think I'd be more likely to want to report an error
to the user than to blindly return a default value.

If I did want to do this, I'd be happy to write my
own function for it.

It could even be made generic:

    def convert(text, func, default):
       try:
          return func(text)
       except ValueError:
          return default

-- 
Greg



More information about the Python-ideas mailing list