[IPython-dev] Output of, e.g., type(a)

MinRK benjaminrk@gmail....
Sat Oct 22 13:22:06 CDT 2011


On Sat, Oct 22, 2011 at 10:49, Warren Weckesser <
warren.weckesser@enthought.com> wrote:

> Hi all,
>
> ipython is doing some magic with the output of 'type(a)' (using 0.11.rc1 in
> EPD):
>
> In [1]: a = 1
>
> In [2]: type(a)
> Out[2]: int
>
> In [3]: str(type(a))
> Out[3]: "<type 'int'>"
>
> In [4]: repr(type(a))
> Out[4]: "<type 'int'>"
>
> The output in line 2 is nicer, but it makes it appear that the builtin
> 'type' behaves in a way that it does not.  My attempts (possibly too brief)
> to find documentation or discussion of this feature have not been fruitful.
> Can anyone point me to the relevant docs, or mailing list discussions?
> Thanks.
>

To turn of *all* pretty printing, use %pprint to toggle (as I now see you
have discovered)

output formatting is all handled by the new display system, which is
unfortunately not very well documented, but is implemented primarily in
IPython.core.formatters<http://ipython.org/ipython-doc/dev/api/generated/IPython.core.formatters.html>
.

But here's the gist:

IPython has a collection of various formatters for various representations
of objects (svg, png, html, latex, plaintext, etc.).  Terminal IPython
obviously only uses plaintext.  To get the relevant formatter in the
terminal:

    formatter = get_ipython().display_formatter.formatters['text/plain']

And this has a dictionary by *type* of functions to call for given types:

    formatter.type_printers

And you can register new type printers with:

    formatter.for_type(type, myfunc)

Where myfunc needs to obey the `(obj,p,cycle)` pattern in
IPython.lib.pretty<http://ipython.org/ipython-doc/dev/api/generated/IPython.lib.pretty.html>
.

If you don't like a particular type's pretty output, you can force it to
fallback on repr by just deleting the relevant key from the type_printers
dict, or explicitly registering a new printer that writes repr:

    formatter.for_type(type, lambda obj, p, cycle: p.text(repr(obj))

-MinRK


> Warren
>
>
> _______________________________________________
> IPython-dev mailing list
> IPython-dev@scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/ipython-dev/attachments/20111022/d66e0966/attachment.html 


More information about the IPython-dev mailing list