[Python-ideas] Improve readability of long numeric literals

Steven D'Aprano steve at pearwood.info
Tue Feb 9 19:50:41 EST 2016


On Wed, Feb 10, 2016 at 12:16:43AM +0000, Oscar Benjamin wrote:

> _123 is currently a valid identifier:
> 
> >>> _123 = 1
> >>> _123
> 1
> 
> 123_ is not. There's no good reason to allow either though. If the
> purpose is to separate the digits for clarity then the underscore
> doesn't need to be at the beginning or the end.

Agreed. 

Disallow leading and trailing underscores, otherwise allow and ignore 
any number of underscores in integer literals so that all of these are 
legal:

123_456_789
0x1234_ABCD
0b1111_0000_1010_0101
0o12_34

For avoidance of doubt, there must be at least one digit before the 
first underscore. These are not allowed:

-_123_456
+_123_456

(actually, they are allowed, since they're legal identifiers).

Consecutive underscores will be allowed:

1234____5678

but the docs (PEP 8?) should say "don't do that". Likewise for excessive 
underscores:

1_2_3_4_5_6_7_8_9_0

These sorts of abuses are a style issue, not a syntax issue.

Floats are more complex:

123_456.000_001e-23

looks okay to me, but what about this?

123_456_._000_001_e_-_23


I think that's ugly. Should we restrict underscores to being only 
between digits? Or just call that a style issue too?



-- 
Steve


More information about the Python-ideas mailing list