Working with python interactively, it's sometimes necessary to display a result which is some arbitrarily complex data structure (like lists with embedded lists, etc.) The default way to display them is just one massive linear dump which just wraps over and over and you have to parse carefully to read it.
Is there something that will take any python object and display it in a more rational manner. e.g.
[0, 1,
[a, b, c],
2, 3, 4]
instead of:
[0, 1, [a, b, c], 2, 3, 4]
I know that's not a very good example, but I think you get the idea.
Another good option is to use IPython, which is an interactive environment with a lot of extra features, including automatic pretty printing, tab-completion of methods, easy shell access, and a lot more. It's also very easy to install.
IPython tutorial
Note that for a short list like my example, pprint will in fact print it all on one line. However, for more complex structures it does a pretty good job of pretty printing data.
Somtimes YAML can be good for this.
Produces:
In addition to
pprint.pprint
,pprint.pformat
is really useful for making readable__repr__
s. My complex__repr__
s usually look like so: