Default return values to int and float
3 Oct
2011
3 Oct
'11
7:52 a.m.
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
4545
Age (days ago)
4551
Last active (days ago)
59 comments
23 participants
participants (23)
-
Bruce Leban
-
Carl Matthew Johnson
-
Chris Rebert
-
David Townshend
-
Dirkjan Ochtman
-
Ethan Furman
-
Greg Ewing
-
Guido van Rossum
-
Jan Kaliszewski
-
Jim Jewett
-
Laurens Van Houtven
-
Mark Dickinson
-
Masklinn
-
Massimo Di Pierro
-
Matt Joiner
-
Michael Foord
-
MRAB
-
Nick Coghlan
-
Paul Moore
-
Raymond Hettinger
-
Ron Adam
-
Steven D'Aprano
-
Terry Reedy