Python’s interactive mode is one of the implementation’s greatstrengths – being able to write expressions on the command lineand get back a meaningful output. However, the output functioncannot be all things to all people, and the current outputfunction too often falls short of this goal. This PEP describes away to provides alternatives to the built-in display function inPython, so users will have control over the output from theinteractive interpreter.
The current Python solution has worked for many users, and thisshould not break it. Therefore, in the default configuration,nothing will change in the REPL loop. To change the way theinterpreter prints interactively entered expressions, userswill have to rebindsys.displayhook to a callable object.The result of calling this object with the result of theinteractively entered expression should be print-able,and this is what will be printed onsys.stdout.
The bytecodePRINT_EXPR will callsys.displayhook(POP()).Adisplayhook() will be added to the sys builtin module, which isequivalent to:
import __builtin__def displayhook(o): if o is None: return __builtin__._ = None print `o` __builtin__._ = o
The methodPy.printResult will be similarly changed.
Source:https://github.com/python/peps/blob/main/peps/pep-0217.rst
Last modified:2025-02-01 08:55:40 GMT