
IDK what you'd grep for in these; if it's not there you could send a PR that adds more greppable keywords or cross-:ref:'s the sys.displayhook docs like this: :func:`sys.displayhook` https://docs.python.org/3/library/sys.html#sys.displayhook https://github.com/python/cpython/blob/master/Doc/library/sys.rst ```rst .. function:: displayhook(value) If *value* is not ``None``, this function prints ``repr(value)`` to ``sys.stdout``, and saves *value* in ``builtins._``. If ``repr(value)`` is not encodable to ``sys.stdout.encoding`` with ``sys.stdout.errors`` error handler (which is probably ``'strict'``), encode it to ``sys.stdout.encoding`` with ``'backslashreplace'`` error handler. ``sys.displayhook`` is called on the result of evaluating an :term:`expression` entered in an interactive Python session. The display of these values can be customized by assigning another one-argument function to ``sys.displayhook``. Pseudo-code:: def displayhook(value): if value is None: return # Set '_' to None to avoid recursion builtins._ = None text = repr(value) try: sys.stdout.write(text) except UnicodeEncodeError: bytes = text.encode(sys.stdout.encoding, 'backslashreplace') if hasattr(sys.stdout, 'buffer'): sys.stdout.buffer.write(bytes) else: text = bytes.decode(sys.stdout.encoding, 'strict') sys.stdout.write(text) sys.stdout.write("\n") builtins._ = value .. versionchanged:: 3.2 Use ``'backslashreplace'`` error handler on :exc:`UnicodeEncodeError`. ``` https://github.com/python/cpython/blob/master/Doc/library/pprint.rst https://github.com/python/cpython/blob/master/Doc/library/reprlib.rst On Thu, Apr 2, 2020, 7:57 PM Steven D'Aprano <steve@pearwood.info> wrote:
On Thu, Apr 02, 2020 at 07:18:47PM +0400, Abdur-Rahmaan Janhangeer wrote:
Out of curiosity how did you learn about sys.displayhook?
20+ years of using Python, experimenting at the interpreter, reading the documentation, blog posts, etc. I have no idea *specifically* where I learned it.
I didn't remember the name of the hook, but I knew it existed, so I ran dir(sys) to get a list of objects in the sys module, then tested it to make sure it did what I thought it did.
-- Steven _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/DMKA3C... Code of Conduct: http://python.org/psf/codeofconduct/