I agree - the three builtin methods are almost the same (not sure if there is any difference at all), while there is no trivial way to check for a valid float, or otherwise a chosen representation of a decimal number without resorting to a try-except statement, or complicated verification schemes that have to deal with a lot of corner cases. For validity one has to check if there are only digits - and decimal points - and the "-" unary sign. What if there is more of a "." in the string? what if the "-" is not the first character?
And besides validity checking, there are also validity choices: What if an unary "+" is present? Whitespace ok? "_" as digit separator ok? scientific exponential notation accepted? What about Inf and Nan literals? What about taking into account the locale setting? But maybe, instead of yet another str method, a "parsefloat" function that could get arguments and sensitive defaults for some choices - it could live in "math" or "numbers". I think it would be preferable than, say, adding a lot of options to the `float` constructor. These are the options I can think of the top of my mind: ```python def parsefloat( input: str, /, *, unary_minus: Bool = True, unary_plus: Bool = False, exponential: Bool = False, inf_literal: Bool = False, nan_literal: Bool = False, whitespace: Bool = False, use_locale: Bool = False, digit_separators: Bool = False, base: int = 10 (?), decimal_places: Optional[int]=None(?), enforce_decimal_places: Bool = False(?), raise_on_error: Bool = False(?), ...) -> Tuple[Bool, float]: ... ``` The return value would consitss of a boolean, where True would indicate success, and then the number. Or it could return a single float, returning a NaN in case of parsing error. this is simple callable - but it could be built on top of a class that could take the configurations and parse more than one number - it would be fit as a float parser to be supplied for tasks like Json decoding, or converting values in a Pandas Series. On Sun, 27 Dec 2020 at 14:12, Tushar Sadhwani <tushar.sadhwani...@gmail.com> wrote: > str currently has methods like isdecimal, isnumeric and isdigit, but there > isn't an isfloat check, which could be very handy. > > a trivial implementation could be as simple as: > > try: > float(self) > return True > except ValueError: > return False > _______________________________________________ > Python-ideas mailing list -- python-ideas@python.org > To unsubscribe send an email to python-ideas-le...@python.org > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/python-ideas@python.org/message/KEUR54FVEE7FRY4RGLK4IGTJOTKXQH5H/ > Code of Conduct: http://python.org/psf/codeofconduct/ >
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/7FJVCF746GOBTM5OD2TGRUCBGB2VSTIG/ Code of Conduct: http://python.org/psf/codeofconduct/