[Python-Dev] PEP 515: Underscores in Numeric Literals

Martin Panter vadmium+py at gmail.com
Wed Feb 10 20:16:16 EST 2016


I have occasionally wondered about this missing feature.

On 10 February 2016 at 22:20, Georg Brandl <g.brandl at gmx.net> wrote:
> Abstract and Rationale
> ======================
>
> This PEP proposes to extend Python's syntax so that underscores can be used in
> integral and floating-point number literals.

This should extend complex or imaginary literals like 10_000j for consistency.

> Specification
> =============
>
> * Trailing underscores are not allowed, because they look confusing and don't
>   contribute much to readability.
> * 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``).
> [. . .]
>
> The production list for integer literals would therefore look like this::
>
>    integer: decimalinteger | octinteger | hexinteger | bininteger
>    decimalinteger: nonzerodigit [decimalrest] | "0" [("0" | "_")* "0"]
>    nonzerodigit: "1"..."9"
>    decimalrest: (digit | "_")* digit
>    digit: "0"..."9"
>    octinteger: "0" ("o" | "O") (octdigit | "_")* octdigit
>    hexinteger: "0" ("x" | "X") (hexdigit | "_")* hexdigit
>    bininteger: "0" ("b" | "B") (bindigit | "_")* bindigit
>    octdigit: "0"..."7"
>    hexdigit: digit | "a"..."f" | "A"..."F"
>    bindigit: "0" | "1"
>
> For floating-point literals::
>
>    floatnumber: pointfloat | exponentfloat
>    pointfloat: [intpart] fraction | intpart "."
>    exponentfloat: (intpart | pointfloat) exponent
>    intpart: digit (digit | "_")*

This allows trailing underscores such as 1_.2, 1.2_, 1.2_e-5. Your
bullet point above suggests at least some of these are not desired.

>    fraction: "." intpart
>    exponent: ("e" | "E") "_"* ["+" | "-"] digit [decimalrest]

This allows underscores in the exponent (1e-5_0), contradicting the
other bullet point.


More information about the Python-Dev mailing list