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

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 10_000j 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.2_e-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-5_0), 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