This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Improve REPL with the underscore separators for int/float
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.11
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: alexprengere, eric.smith
Priority: normal Keywords:

Created on 2021-11-24 15:55 by alexprengere, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg406934 - (view) Author: Alex (alexprengere) * Date: 2021-11-24 15:55
I often use to the Python REPL to perform some simple calculations (typically some combinatorial or probabilities computation).
I believe it would be a nice improvement if the number displayed in the REPL would be formatted as if f"{result:_}" was given (with the underscore separators). For example:

 >>> 36 ** 7
 78_364_164_096

As I understand things:
* the REPL always shows the __repr__
* updating the __repr__ for int/float is a no-no for backward compatibility reasons

If these assumptions are correct (please correct me if this is wrong), then I guess this cannot be implemented, unless I missed something?
msg406937 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-11-24 16:38
We can't change the repr of int/float.

However, you can use sys.displayhook to achieve what you want:

import sys

def displayhook(o):
    if o is None:
        return
    __builtins__._ = None
    if isinstance(o, (int, float)):
        print(format(o, '_'))
    else:
        print(repr(o))
    __builtins__._ = o

sys.displayhook = displayhook

Then:

>>> 12312312
12_312_312
>>> 123123e123
1.23123e+128
>>> None
>>> 'test'
'test'
msg406938 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-11-24 16:39
Oops, the float example should be:

>>> 123123.9
123_123.9
msg406969 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-11-24 23:38
And you can probably use sitecustomize.py to import this.

Since I don't see any action item here, I'm going to close this issue.
msg406982 - (view) Author: Alex (alexprengere) * Date: 2021-11-25 09:29
Thanks a lot Eric for your answers, I did not know about sys.displayhook.
Now I know I can easily implement this myself, but the larger question would be: "do we want to update the default displayhook for those simple use cases".
History
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 90050
2021-11-25 09:29:28alexprengeresetmessages: + msg406982
2021-11-24 23:38:12eric.smithsetstatus: open -> closed
resolution: not a bug
messages: + msg406969

stage: resolved
2021-11-24 16:39:09eric.smithsetmessages: + msg406938
2021-11-24 16:38:33eric.smithsetnosy: + eric.smith
messages: + msg406937
2021-11-24 15:55:50alexprengerecreate