[Python-ideas] 1_000_000

Philip Jenvey pjenvey at underboss.org
Sat May 7 01:59:35 CEST 2011


On May 6, 2011, at 4:41 PM, MRAB wrote:

> On 06/05/2011 23:51, Cameron Simpson wrote:
>> On 06May2011 15:40, Ethan Furman<ethan at stoneleaf.us>  wrote:
>> | Bruce Leban wrote:
>> |>Is _ just ignored in numbers or are there more complex rules?
>> |>
>> |>     * 1_2345_6789  (can I use groups of other sizes instead?)
>> |>     * 1_2_3_4_5  (ditto)
>> |>     * 1_234_6789  (do all the groups need to be the same size?)
>> |>     * 1_   (must the _ only be in between 2 digits?)
>> |>     * 1__234   (what about multiple _s?)
>> |>     * 9.876_543_210   (can it be used to the right of the decimal point?)
>> |>     * 0xFEFF_0042   (can it be used in hex, octal or binary numbers?)
>> |>     * int('123_456')   (do other functions accept this syntax too?)
>> |
>> | I would say it's ignored.  Have the rule be something like
>> | number_string.replace('_','').
>> |
>> | The only wrinkle is that currently '_1' is usable name, and that
>> | should probably be disallowed if the above change took place.
>> |
>> | I'm +1 on the idea.
>> 
>> Personally I'm be for ignoring the _ also, save that I would forbid it
>> at the start or end, so no _1 or 1_.
>> 
>> And I would permit it in hex code etc.
>> 
>> I'm +0.5, myself.
>> 
> As far as I remember, Ada also permits it, but has the rule that it can
> occur only between digits. If we follow that, then:
> 
>    1_2345_6789 => Yes
>    1_2_3_4_5 => Yes
>    1_234_6789 => Yes
>    1_ => No
>    _1 => No
>    1__234 => No
>    9.876_543_210 => Yes
>    9._876_543_210 => No
>    9_.876_543_210 => No
>    0xFEFF_0042 => Yes
>    int('123_456') => Yes

Java 7 also adds this feature. Its rules:

You can place underscores only between digits; you cannot place underscores in the following places:

	• At the beginning or end of a number
	• Adjacent to a decimal point in a floating point literal
	• Prior to an F or L suffix
	• In positions where a string of digits is expected
The following examples demonstrate valid and invalid underscore placements in numeric literals:

float pi1 = 3_.1415F;      // Invalid; cannot put underscores adjacent to a decimal point
float pi2 = 3._1415F;      // Invalid; cannot put underscores adjacent to a decimal point
long socialSecurityNumber1
  = 999_99_9999_L;         // Invalid; cannot put underscores prior to an L suffix

int x1 = _52;              // This is an identifier, not a numeric literal
int x2 = 5_2;              // OK (decimal literal)
int x3 = 52_;              // Invalid; cannot put underscores at the end of a literal
int x4 = 5_______2;        // OK (decimal literal)

int x5 = 0_x52;            // Invalid; cannot put underscores in the 0x radix prefix
int x6 = 0x_52;            // Invalid; cannot put underscores at the beginning of a number
int x7 = 0x5_2;            // OK (hexadecimal literal)
int x8 = 0x52_;            // Invalid; cannot put underscores at the end of a number

int x9 = 0_52;             // OK (octal literal)
int x10 = 05_2;            // OK (octal literal)
int x11 = 052_;            // Invalid; cannot put underscores at the end of a number

(From http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html )

--
Philip Jenvey




More information about the Python-ideas mailing list