displayhook = pprint & _ not happy together

Alex Martelli aleax at aleax.it
Mon Aug 27 05:33:47 EDT 2001


"Sandy Norton" <sandskyfly at hotmail.com> wrote in message
news:b03e80d.0108260540.15584b05 at posting.google.com...
> From the documentation:
>
> "In interactive mode, the last printed expression is assigned to the
> variable _. This means that when you are using Python as a desk

That's part of the job of the displayhook, of course.

> import sys, pprint
> sys.displayhook = pprint.pprint
> del pprint, sys
>
> which works fine in regular cases and gives me pretty-printed
> interactive output (which I dearly like). But I've just noticed that
> doing this makes the _ non-functional. For example:

...because pprint knowns nothing about it, of course.

> Bug or feature?

Feature.  Easy to fix if you want both pretty printing
AND the _ assignment, of course:

import sys, pprint, __builtin__
def myhook(value, show=pprint.pprint, bltin=__builtin__):
    if value is not None:
        bltin._ = value
        show(value)
sys.displayhook = myhook
del __builtin__, pprint, sys

or whatever exact logic you wish.  That's better than
using pprint.pprint directly and getting a None displayed
each time you interactively call a subroutine, anyway:-).


Alex







More information about the Python-list mailing list