On Tue, Sep 12, 2017 at 9:20 PM, Steven D'Aprano firstname.lastname@example.org wrote:
On Mon, Sep 11, 2017 at 06:26:16PM -0600, Neil Schemenauer wrote:
On 2017-09-12, Victor Stinner wrote:
Instead of modifying the Python grammar, the alternative is to enhance float(str) to support it:
k = float("0x1.2492492492492p-3") # 1/7
Making it a different function from float() would avoid backwards compatibility issues. I.e. float() no longer returns errors on some inputs.
I don't think many people will care about backwards compatibility of errors. Intentionally calling float() in order to get an exception is not very common (apart from test suites). Its easier to use raise if you want a ValueError.
The only counter-example I can think of is beginner programmers who write something like:
num = float(input("Enter a number:"))
and are surprised when the "invalid" response "0x1.Fp2" is accepted. But then they've already got the same so-called problem with int accepting "invalid" strings like "0xDEADBEEF". So I stress that this is a problem in theory, not in practice.
Your specific example doesn't work as int() won't accept that by default - you have to explicitly say "base=0" to make that acceptable. But we have other examples where what used to be an error is now acceptable:
Python 3.5.3 (default, Jan 19 2017, 14:11:04) [GCC 6.3.0 20170118] on linux Type "help", "copyright", "credits" or "license" for more information.
int("1_234_567") Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: '1_234_567'
Python 3.7.0a0 (heads/master:cb76029b47, Aug 30 2017, 23:43:41) [GCC 6.3.0 20170516] on linux Type "help", "copyright", "credits" or "license" for more information.
Maybe hex floats should be acceptable only with float(str, base=0)?