[Python-Dev] PEP 515: Underscores in Numeric Literals (original) (raw)

Georg Brandl g.brandl at gmx.net
Thu Feb 11 02:25:24 EST 2016


On 02/11/2016 02:16 AM, Martin Panter wrote:

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 10000j for consistency.

Yes, that was always the case, but I guess it should be explicit.

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.2e-5. Your bullet point above suggests at least some of these are not desired.

The middle one isn't, indeed. I updated the grammar accordingly.

fraction: "." intpart exponent: ("e" | "E") ""* ["+" | "-"] digit [decimalrest] This allows underscores in the exponent (1e-50), contradicting the other bullet point.

I clarified the bullet points. An "immediately" was missing.

Thanks for the feedback! Georg



More information about the Python-Dev mailing list