Repository navigation
Float documentation doesn't allow int-like strings as valid arguments for float #100428
Description
Activity
Maybe remove the indirection (the reference to floating-point literals) from the current documentation and give a direct description of what's permitted instead? That way the information is all in one place. The current documentation feels as though it was a shortcut taken for ease of documentation writing rather than ease of documentation reading.
On the other hand, it's definitely useful for users to know that any string that's acceptable as a floating-point literal is also acceptable as a string argument to
float.Another way in which the indirection is inaccurate is that
floataccepts arbitrary Unicode digits, while literals may only use ASCII digits:>>> s = '666.0' >>> float(s) 666.0 >>> eval(s) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1 666.0 ^ SyntaxError: invalid character '6' (U+FF16)
There are inaccuracies in the
intdocumentation too, for instance, the handling of leading zeros isn't really described. Also the following isn't correct:Base 0 means to interpret exactly as a code literal
>>> s = "२३४५६७८९" >>> int(s, 0) 23456789 >>> eval(s) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1 २३४५६७८९ ^ SyntaxError: invalid character '२' (U+0968)- added 4 commits that reference this issue
on Dec 24, 2022 - added a commit that references this issue
on Dec 28, 2022 - added 4 commits that reference this issue
on Jan 2, 2023
The documentation for
floatdoesn't explicitly allow int-like strings, although it does give one as an example. E.g. althoughfloat('10')works and returns a float, the documentation implies that it shouldn't.See discussion on Discuss.
Linked PRs