[Python-ideas] Default return values to int and float
Laurens Van Houtven
_ at lvh.cc
Mon Oct 3 12:42:41 CEST 2011
I don't think David is arguing for the default behavior to change -- merely that you get a dict.get style default. Kinda similar to getattr/2 raising AttributeError, and getattr/3 returning the default value.
cheers
lvh
On 03 Oct 2011, at 12:40, Greg Ewing wrote:
> 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
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1306 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20111003/ccd33b2d/attachment.bin>
More information about the Python-ideas
mailing list