Skip to content

bpo-42914: pprint.pprint function displays integer with underscores #24864

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

Merged
merged 12 commits into from
Mar 24, 2021

Conversation

sblondon
Copy link
Contributor

@sblondon sblondon commented Mar 14, 2021

This PR implements the separation for big integers by _ character for better readability for pprint.pprint() and pprint.pformat(). So 123456 is displayed as '1_234_56'.

A new parameter (underscore_numbers) is added so this new behavior can be disabled.

https://bugs.python.org/issue42914

@sblondon sblondon force-pushed the pprint_number_with_underscores branch from af908b7 to dd308b0 Compare March 16, 2021 16:09
Lib/pprint.py Outdated

if issubclass(typ, int) and r is int.__repr__:
if self._underscore_numbers:
return builtins.format(object, "_d"), True, False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use f"{object:_d}" instead of format? That'll be less bytecode and thus faster.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It uses builtins.format() according ericvsmith's review (#24864 (comment)): the builtins.format() function was considered lightly better than f-string format.

Format documentation shows '%-format' is not the preferred way of formating. builtins.format() and f-string format use the same Format Specification Mini-Language but there is not an official preference between them (or I did not find it in the documentation). Do you know if there is an official opinion about it? Should I ask on python-dev mailing list?

I'm ready to change to f-string format but I'm afraid another commiter has another opinion about this point and ask to revert it again.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f-strings would be best. I was commenting on the built-in format() vs str.format().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the explanation. :)
I committed a change to use f-string format.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@sblondon sblondon force-pushed the pprint_number_with_underscores branch from 1a229e3 to 57ff2b7 Compare March 22, 2021 13:39
@sblondon
Copy link
Contributor Author

I changed the default behavior to keep backward compatibility (requested by https://bugs.python.org/msg389205).

@sblondon
Copy link
Contributor Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@gpshead: please review the changes made to this pull request.

@bedevere-bot bedevere-bot requested a review from gpshead March 22, 2021 14:24
@gpshead gpshead added the type-feature A feature request or enhancement label Mar 24, 2021
@gpshead gpshead self-assigned this Mar 24, 2021
@gpshead gpshead merged commit 3ba3d51 into python:master Mar 24, 2021
@sblondon sblondon deleted the pprint_number_with_underscores branch March 24, 2021 14:17
miss-islington pushed a commit that referenced this pull request Jun 3, 2021
BPO-42914 was not added to the What's New in #24864. This includes it in the "Improved Modules" section.

Automerge-Triggered-By: GH:gpshead
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 3, 2021
BPO-42914 was not added to the What's New in pythonGH-24864. This includes it in the "Improved Modules" section.

Automerge-Triggered-By: GH:gpshead
(cherry picked from commit 4846ea9)

Co-authored-by: Wm. Keith van der Meulen <[email protected]>
miss-islington added a commit that referenced this pull request Jun 3, 2021
BPO-42914 was not added to the What's New in GH-24864. This includes it in the "Improved Modules" section.

Automerge-Triggered-By: GH:gpshead
(cherry picked from commit 4846ea9)

Co-authored-by: Wm. Keith van der Meulen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants