[Python-Dev] PEP 515: Underscores in Numeric Literals
Steven D'Aprano
steve at pearwood.info
Wed Feb 10 19:04:27 EST 2016
On Wed, Feb 10, 2016 at 11:20:38PM +0100, Georg Brandl wrote:
> This came up in python-ideas, and has met mostly positive comments,
> although the exact syntax rules are up for discussion.
Nicely done. But I would change the restrictions to a simpler version.
Instead of five rules to learn:
> The current proposal is to allow underscores anywhere in numeric literals, with
> these exceptions:
>
> * Leading underscores cannot be allowed, since they already introduce
> identifiers.
> * Trailing underscores are not allowed, because they look confusing and don't
> contribute much to readability.
> * The number base prefixes ``0x``, ``0o``, and ``0b`` cannot be split up,
> because they are fixed strings and not logically part of the number.
> * No underscore allowed after a sign in an exponent (``1e-_5``), because
> underscores can also not be used after the signs in front of the number
> (``-1e5``).
> * No underscore allowed after a decimal point, because this leads to ambiguity
> with attribute access (the lexer cannot know that there is no number literal
> in ``foo._5``).
change to a single rule "one or more underscores may appear between two
(hex)digits, but otherwise nowhere else". That's much simpler to
understand than a series of restrictions as given above.
That would be your second restrictive rule:
"Multiple consecutive underscore allowed, but only between digits."
That forbids leading and trailing underscores, underscores inside or
immediately after the leading number base (since x, o and b aren't
digits), and immediately before or after the sign, decimal point or e|E
exponent symbol.
> There appears to be no reason to restrict the use of underscores otherwise.
I don't like underscores immediately before the . or e|E in floats
either: 123_.000_456
The dot is already visually distinctive enough, as is the e|E, and
placing an underscore immediately before them doesn't aid in grouping
the digits.
> Instead of the liberal rule specified above, the use of underscores could be
> limited. Common rules are (see the "other languages" section):
>
> * Only one consecutive underscore allowed, and only between digits.
> * Multiple consecutive underscore allowed, but only between digits.
I don't think there is any need to restrict it to only a single
underscore. There are uses for more than one:
Fraction(3__141_592_654, 1_000_000_000)
hints that the 3 is somewhat special (for obvious reasons).
--
Steve
More information about the Python-Dev
mailing list