Underlining: The use of underlining in Python is amazing, and you’ve probably seen it in various articles and tutorials. After version 3.6 in 2016, Python also introduced a new syntax that allowed underscores to appear in numeric values. This translated document will walk you through the introduction of this feature.
The original PEP: www.python.org/dev/peps/pe…
PEP title: PEP 515 — Assumptions in Numeric Literals
PEP by Guido Van Rossum and Nick Coghlan
Created by Georg Brandl, Serhiy Storchaka
Integrated version: 3.6
The cat under the pea flower
PEP Translation Program: github.com/chinesehuaz…
Overview and Principles
This PEP proposes to extend Python’s syntax so that in the number-from-string constructor, underscores can be used as visual separators to group numbers of integer, floating point, and complex literals.
(Python Cat Note: For Python numeric types, see PEP-3141.)
This is a common feature of other modern languages and helps to understand that long or valued literals should be visually divided into parts, such as bytes or words in hexadecimal notation.
Example:
# grouping decimal numbers by thousands
amount = 10 _000_000. 0
# grouping hexadecimal addresses by words
addr = 0xCAFE_F00D
# grouping bits into nibbles in a binary literal
flags = 0b_0011_1111_0100_1110
# same, for string conversions
flags = int('0b_1111_0000'.2)
Copy the code
specification
The current proposal is to allow an underscore between numbers and after the base identifier of numeric literals. Underscores have no semantic meaning, and numeric literals are parsed as if there were no underscores.
Literal grammar
Thus, the notation for integer literals looks like this:
integer: decinteger | bininteger | octinteger | hexinteger
decinteger: nonzerodigit (["_"] digit)* | "0" (["_"] "0")*
bininteger: "0" ("b" | "B") (["_"] bindigit)+
octinteger: "0" ("o" | "O") (["_"] octdigit)+
hexinteger: "0" ("x" | "X") (["_"] hexdigit)+
nonzerodigit: "1"."9"
digit: "0"."9"
bindigit: "0" | "1"
octdigit: "0"."Seven"
hexdigit: digit | "a"."f" | "A"."F"
Copy the code
Floating-point and complex number literals:
floatnumber: pointfloat | exponentfloat
pointfloat: [digitpart] fraction | digitpart "."
exponentfloat: (digitpart | pointfloat) exponent
digitpart: digit (["_"] digit)*
fraction: "." digitpart
exponent: ("e" | "E") ["+" | "-"] digitpart
imagnumber: (floatnumber | digitpart) ("j" | "J")
Copy the code
The constructor
Following the same placement rules, underscores can be used in the following constructors:
- Int () (arbitrary base)
- float()
- complex()
- Decimal()
Further changes
The new number-to-string formatting syntax will be extended to allow _ as a thousand separator. This makes it easy to generate code with more readable literals. [11]
Separator The syntax would be The same as for The comma, e.g. {:10_} for a width of 10 with _ separator. No translation)
For b, X, and O formats, _ will also be supported and grouped by 4 digits.
Existing technology
Languages that allow grouping of underscores implement a number of rules for placing underscores. In cases where language norms contradict actual behavior, actual behavior is listed below. (” Single “or” multiple “refers to how many consecutive underscores are allowed.)
- Ada: Single, only between numbers [8]
- C# (version 7.0 proposal) : multiple, only between numbers [6]
- C++14: single, between numbers (with other delimiters selected) [1]
- D: Multiple, arbitrary positions, including the end [2]
- Java: multiple, only between numbers [7]
- Julia: Single, only between numbers (but not the floating-point index part) [9]
- Perl 5: Multiple, basically anywhere, although the documentation says there is a limit of 1 underscore between digits [3]
- Ruby: Single, only between numbers (although the documentation says “anywhere”) [10]
- Rust: multiple, anywhere except between exponent “e” and number [4]
- Swift: multiple, between digits and trailing (although the documentation says only “between digits”) [5]
Rejected syntax
(Python cat note: Each PEP in its inception phase can lead to a lot of discussion about syntactic design, and in formally adopted PEPs, there are typically some rejected alternatives, such as the following two.)
1, underline placement rules
Reduce the restrictions on the use of underscores rather than the relatively strict rules declared above. In other languages, common rules include:
- Only one consecutive underscore is allowed, and only between digits.
- Multiple consecutive underscores are allowed, but only between digits.
- Multiple consecutive underscores are allowed in most places, except at the beginning of a literal, or in special places (such as after a decimal point).
The syntax in this PEP was ultimately selected because it covers common use cases and does not present syntax that would be discouraged by the Python style guide.
A less common rule is to only allow underscores for every N digits (where N may be three decimal literals, or four hexadecimal literals). This is unnecessarily restrictive, especially considering that these delimiter locations are different in different cultures. (Python cat note: For example, in my country it is customary to divide four numbers into groups, i.e. 10,000 is 10,000, not 10 thousand as in English culture)
2. Other delimiters
Another suggestion is to use Spaces for grouping. While strings are a precedent for combining adjacent literals, this behavior can lead to unintended effects, whereas underscores do not. Furthermore, no other languages use this rule except those that basically ignore any whitespace.
C++ 14 introduced single quotes for grouping (because underlining would confuse user-defined literals), and since single quotes were already used in Python string literals, they were not considered. [1]
implementation
A preliminary patch implementing the above specification has been released to the issue tracker. [12]
Refer to the content
[1] (1, 2) www.open-std.org/jtc1/sc22/w…
[2] dlang.org/spec/lex.ht…
[3] perldoc.perl.org/perldata#Sc…
[4] web.archive.org/web/2016030…
[5] docs.swift.org/swift-book/…
[6] github.com/dotnet/rosl…
[7] docs.oracle.com/javase/7/do…
[8] archive.adaic.com/standards/8…
[9] web.archive.org/web/2016022…
[10] ruby-doc.org/core-2.3.0/…
[11] mail.python.org/pipermail/p…
[12] bugs.python.org/issue26331
copyright
The document is in the public domain.
Source file: github.com/python/peps…