How to use digit separators for Python integer literals?
Asked Answered
W

4

51

Is there any way to group digits in a Python code to increase code legibility? I've tried ' and _ which are digit separators of some other languages, but no avail.

A weird operator which concatenates its left hand side with its right hand side could also work out.

Woodruff answered 1/7, 2016 at 23:58 Comment(6)
can you be more specific.. give an example?Microclimate
I think he wants to replace the integer literal 31415926 with 31_415_926, for example.Chaparajos
I'm not sure that's implemented. There are proposals, but not implementationsElbowroom
int('100,000'.replace(',', '')) is ok?Smote
Guido mentioned this in this years pycon keynote youtu.be/YgtL4S7Hrwo?t=428, its coming in 3.6Celeski
I though that the link would be enough to make it clear. Example that Prune gave fits perfectly.Woodruff
C
59

Update a few years later: Python 3.6 now supports PEP515, and so you can use _ for float and integer literal readability improvement.

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 1_1000
11000
>>>

For historical reference, you can look at the lexical analysis for strict definitions python2.7, python3.5 ...

For python3.6.0a2 and earlier, you should get an error message similar to:

Python 3.6.0a2 (v3.6.0a2:378893423552, Jun 13 2016, 14:44:21) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 1_000
  File "<stdin>", line 1
    1_000
        ^
SyntaxError: invalid syntax
>>> amount = 10_000_000.0
  File "<stdin>", line 1
    amount = 10_000_000.0
                      ^
SyntaxError: invalid syntax
Camellia answered 2/7, 2016 at 0:3 Comment(0)
V
5

Currently there is no thousands separator in Python, but you can use locale module to convert string with such separators to an int:

import locale
locale.setlocale(locale.LC_ALL, '')
locale.atoi("1,000,000")
Viniferous answered 2/7, 2016 at 0:15 Comment(0)
K
3

There is no such function in Python but it was proposed to integrate it in the future.

You can see the proposal in the PEP515.

Krypton answered 2/7, 2016 at 0:3 Comment(0)
A
2

The closest thing I've seen in python is 12 * 1000 * 1000, which is not ideal, but can be useful if 12000000 is needed. Be advised though, while in C, those are equivalent, because at compile time it converts both to the same thing, python may not share this optimization.

Alfieri answered 2/7, 2016 at 0:6 Comment(6)
If they are literals, python will fold the constants. It won't do any folding of symbols though.Camellia
Thanks. I thought it would, but I didn't want to spread bad info.Alfieri
For 12 million, I'd use and recommend 12e6.Woodruff
@ThoAppelsin -- Well, that depends on whether you want your value to be a float or an int...Camellia
@Alfieri -- You can always check these things by disassembling the source: dis.dis(lambda : 100 * 200 * 300)Camellia
@mgilson: As it happens, this is one of those things where order matters a lot. If you use x * 100 * 1000 * 1000, then it won't fold (because x * 100 might not return an int if x isn't an int). But 100 * 1000 * 1000 * x folds, because the left to right evaluation is working with known constants.Golliner

© 2022 - 2024 — McMap. All rights reserved.