Steven D'Aprano wrote: > On Sat, Oct 02, 2021 at 02:17:32AM -0000, Debashish Palit wrote: > > The method need not take care of every variation. Currently there is > > no method to check for a float in a string even without formatting. > > Right, because no method is needed. If you want to convert something to > a float (whether a string, or anything else), it is nearly always > better to try to convert it: > try: > result = float(s) > except TypeError: > # Cannot be converted to a float. > ... # some fallback routine > This software pattern is called "Easier to Ask Forgiveness than > Permission" and is usually more efficient and safer than the opposite > pattern, "Look Before You Leap". > https://devblogs.microsoft.com/python/idiomatic-python-eafp-versus-lbyl/ > So the number of use-cases for your function are probably very few.
I am not against EAFP. It should be used, but when you have decide between an int and a float, you would need to apply it twice. str.float is a convenience method wrapping that. > > > > When accepting user input with the input() function, you may get > > > > integers and not floats. If you just convert the strings to floats, > > you will get the result of a calculation as a float. Here the type of > > the input needs to be preserved. > > Can you give a real example of a case where you are doing calculations > on a number provided as a string, but need to preserve the original > format? Real example =========== Consider a number radix conversion software. It needs to accept the input number as a string (say '15A' in base 12). When we provide it a base 10 number, it would also be a string. If we are storing the base 10 equivalent of the input number, then we need to convert the input number to the correct type - float or int. Otherwise calculations may not be accurate - addition may yield a float when an int was expected. > > After converting the number to a float, you may want > > to output the value in a repr method. Saving the precise type would > > help. > > If you need to preserve the exact input for the repr method, you should > preserve the actual input. > class MyClass: > def __init__(self, astring): > self._arg = astring > self.number = float(astring) > def __repr__(self): > return 'MyClass(%s)' % self._arg Here you are storing the same info twice - as a string and as a number. Why not just store a number? > > The parse_value function would work as well but str.isfloat is more > > inline with the current string methods. > > Not really. There are no string methods to test for: > - ints > - floats > - complex numbers > - fractions > - decimals > you are expected to just try to convert and catch the exception if it > occurs (EAFP). So this would be new. As I have mentioned above, EAFP is fine but you have to apply it twice. str.float accomplishes the same with 1 line of code. num = float(s) if s.float() else int(s) _______________________________________________ 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/O32XDZPUHVUVURVDXDTLNTFPOVCJW23O/ Code of Conduct: http://python.org/psf/codeofconduct/