Update PEP 515 with corrections.

This commit is contained in:
Georg Brandl 2016-02-11 08:45:52 +01:00
parent 345497f028
commit 1202d81471
1 changed files with 34 additions and 12 deletions

View File

@ -13,7 +13,7 @@ Abstract and Rationale
====================== ======================
This PEP proposes to extend Python's syntax so that underscores can be used in This PEP proposes to extend Python's syntax so that underscores can be used in
integral and floating-point number literals. integral, floating-point and complex number literals.
This is a common feature of other modern languages, and can aid readability of This is a common feature of other modern languages, and can aid readability of
long literals, or literals whose value should clearly separate into parts, such long literals, or literals whose value should clearly separate into parts, such
@ -30,6 +30,9 @@ Examples::
# grouping bits into bytes in a binary literal # grouping bits into bytes in a binary literal
flags = 0b_0011_1111_0100_1110 flags = 0b_0011_1111_0100_1110
# making the literal suffix stand out more
imag = 1.247812376e-15_j
Specification Specification
============= =============
@ -43,12 +46,12 @@ these exceptions:
contribute much to readability. contribute much to readability.
* The number base prefixes ``0x``, ``0o``, and ``0b`` cannot be split up, * The number base prefixes ``0x``, ``0o``, and ``0b`` cannot be split up,
because they are fixed strings and not logically part of the number. because they are fixed strings and not logically part of the number.
* No underscore allowed after a sign in an exponent (``1e-_5``), because * No underscore allowed immediately after a sign in an exponent (``1e-_5``),
underscores can also not be used after the signs in front of the number because underscores can also not be used after the signs in front of the
(``-1e5``). number (``-1e5``).
* No underscore allowed after a decimal point, because this leads to ambiguity * No underscore allowed immediately after a decimal point, because this leads to
with attribute access (the lexer cannot know that there is no number literal ambiguity with attribute access (the lexer cannot know that there is no number
in ``foo._5``). literal in ``foo._5``).
There appears to be no reason to restrict the use of underscores otherwise. There appears to be no reason to restrict the use of underscores otherwise.
@ -66,14 +69,28 @@ The production list for integer literals would therefore look like this::
hexdigit: digit | "a"..."f" | "A"..."F" hexdigit: digit | "a"..."f" | "A"..."F"
bindigit: "0" | "1" bindigit: "0" | "1"
For floating-point literals:: For floating-point and complex literals::
floatnumber: pointfloat | exponentfloat floatnumber: pointfloat | exponentfloat
pointfloat: [intpart] fraction | intpart "." pointfloat: [intpart] "_"* "." intpart | intpart "_"* "."
exponentfloat: (intpart | pointfloat) exponent exponentfloat: (intpart | pointfloat) "_"* exponent
intpart: digit (digit | "_")* intpart: digit [(digit | "_")* digit]
fraction: "." intpart
exponent: ("e" | "E") "_"* ["+" | "-"] digit [decimalrest] exponent: ("e" | "E") "_"* ["+" | "-"] digit [decimalrest]
imagnumber: (floatnumber | intpart) "_"* ("j" | "J")
Further Considerations
======================
This PEP currently only proposes changing the literal syntax. The following
extensions are open for discussion:
* Allowing underscores in string arguments to the ``Decimal`` constructor. It
could be argued that these are akin to literals, since there is no Decimal
literal available (yet).
* Allowing underscores in string arguments to ``int()``, ``float()`` and
``complex()``.
Alternative Syntax Alternative Syntax
@ -88,6 +105,11 @@ limited. Common rules are (see the "other languages" section):
* Only one consecutive underscore allowed, and only between digits. * Only one consecutive underscore allowed, and only between digits.
* Multiple consecutive underscore allowed, but only between digits. * Multiple consecutive underscore allowed, but only between digits.
A less common rule would be to allow underscores only every N digits (where N
could be 3 for decimal literals, or 4 for hexadecimal ones). This is
unnecessarily restrictive, especially considering the separator placement is
different in different cultures.
Different Separators Different Separators
-------------------- --------------------