New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pprint numbers with underscore #87080
Comments
It would be nice if pprint learned to insert underscores in long numbers Current behavior: >>> pprint.pprint(int(1e9))
1000000000 Desired behavior >>> pprint.pprint(int(1e9))
1_000_000_000 Wikipedia tells me that "groups of 3" is the international standard to be followed here [1][2] [1] https://en.wikipedia.org/wiki/ISO_31-0#Numbers |
Here is an implementation of the safe repr for numbers if helpful:
|
+1 but I would make this optional.
I suggest using the existing string formatting tools as a foundation >>> format(10**9, ',d').replace(',', '_')
'1_000_000_000' |
+1 also. I agree with Raymond it should be optional. |
>>> format(10**9, '_d')
'1_000_000_000' |
I add the same idea but later than you, so I'm interested by such feature. Felipe: do you want to add a pull request to this issue (with Serhiy Storchaka implementation because it's the simplest one)? If not, I plan to write it. |
All yours! I'm tied up so won't be able to submit the PR On Thu, 25 Feb 2021 at 10:12, Stéphane Blondon <report@bugs.python.org>
|
Thank you Felipe for the news! :) Two remarks:
As requested, there is a new parameter to disable this new behavior ('underscore_numbers'). 1: https://docs.python.org/3/library/string.html#format-specification-mini-language |
I don't think underscores can be on by default. It needs to be opt-in to be backwards compatible. |
I changed the default to be backward compatible (so underscore_numbers=False). I think it would be better with underscore_numbers enabled by default but I understand the need for stability. Perhaps such break could be done in the future (in version 3.12 or v.4)? |
Thanks for the contribution Stéphane! I agree that this would be a nice default. We're just being conservative in the pace of default behavior changes. Changing the default could be considered in the future after a few releases with this parameter have shipped. |
Python 3.10 has now been released with the underscore_numbers parameter.
Could it be accepted before (like 3.12 or 3.13)? If there is no reply, I will create a new issue and PR for 3.14 inclusion ( = safest strategy). |
The safest thing to do is never make it the default. It would always be an opt-in behavior. |
Ok, I will not send a PR to change the current behavior until python4 (in case it exists one day). |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: