Pretty Print
In addition to print()
, Python includes a pretty print method. This method is particularly useful for outputting debugging information about objects in a more easily readable format:
>>> from pprint import pprint
>>> data = {
... 'squares':[x**2 for x in range(10)]
... }
>>> pprint(data)
{'squares': [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]}
>>> data['tens'] = [x**10 for x in range(10)]
>>> print(data)
{'squares': [0, 1, 4, 9, 16, 25, 36, 49, 64, 81], 'tens': [0, 1, 1024, 59049, 1048576, 9765625, 60466176, 282475249, 1073741824, 3486784401]}
>>> pprint(data)
{'squares': [0, 1, 4, 9, 16, 25, 36, 49, 64, 81],
'tens': [0,
1,
1024,
59049,
1048576,
9765625,
60466176,
282475249,
1073741824,
3486784401]}
>>> pprint(data, width=20)
{'squares': [0,
1,
4,
9,
16,
25,
36,
49,
64,
81],
'tens': [0,
1,
1024,
59049,
1048576,
9765625,
60466176,
282475249,
1073741824,
3486784401]}
>>> pprint(data, indent=3, width=20)
{ 'squares': [ 0,
1,
4,
9,
16,
25,
36,
49,
64,
81],
'tens': [ 0,
1,
1024,
59049,
1048576,
9765625,
60466176,
282475249,
1073741824,
3486784401]}
>>> pprint(data, width=40, compact=True)
{'squares': [0, 1, 4, 9, 16, 25, 36, 49,
64, 81],
'tens': [0, 1, 1024, 59049, 1048576,
9765625, 60466176, 282475249,
1073741824, 3486784401]}
>>> cities = {
... 'USA': {'Texas': {'Dallas':['Irving']}},
... 'CANADA': {'BC': {'Vancouver':['North Van']}},
... }
>>> pprint(cities)
{'CANADA': {'BC': {'Vancouver': ['North Van']}},
'USA': {'Texas': {'Dallas': ['Irving']}}}
>>> pprint(cities, depth=3)
{'CANADA': {'BC': {'Vancouver': [...]}}, 'USA': {'Texas': {'Dallas': [...]}}}
>>> items = [1, 2, 3]
>>> items.append(items)
>>> print(items)
[1, 2, 3, [...]]
>>> pprint(items)
[1, 2, 3, <Recursion on list with id=4547300424>]
>>> id(items)
4547300424
>>> pprint('One', 'Two')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pprint.py", line 53, in pprint
printer.pprint(object)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pprint.py", line 139, in pprint
self._format(object, self._stream, 0, 0, {}, 0)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pprint.py", line 176, in _format
stream.write(rep)
AttributeError: 'str' object has no attribute 'write'
00:00
In the previous lesson, I talked about how print()
has changed between Python 2 and Python 3. In this lesson, I’ll be talking about the pprint()
(pretty print) function out of the pprint
(pretty print) library.
00:11
In addition to the built-in print()
function, Python also offers a pretty print function. That’s shortened to pprint()
, and it’s found in the pprint
library.
00:20 Let me just quickly create some data here to show this off.
00:29
And now, calling pprint()
similar to how I would call print()
, it prints out the results of this dictionary. So far, no different than print()
. That’s because pprint()
is based on the line length.
00:42
The amount of data that I have in this dictionary at the moment is less than 80 characters, so there’s no difference to print()
. I can change that by adding some content to the hash.
00:54
Here’s what that would look like in print()
. It’s a long line about a hundred characters long. Now, in pprint()
, it gets printed out in a much easier-to-read format.
01:07
The width of 80
characters is the default, but it can be changed. You can pass in an argument, cleverly enough called width
, and it changes the behavior. Here’s a width of 20
.
01:21
You can also affect the amount of indent that is shown in the printing by changing the indent
argument.
01:33 Or you can put it in compact mode, which allows you sort of a halfway in between the two.
01:44 Bear with me while I put in some more data. I’m going to create a dictionary with some dictionaries and some lists nested inside of it.
02:00
The first pass to pprint()
—no real surprise here, it’s just sort of separated it off into the lines. But you can add a parameter to pprint()
to change what depth it prints out.
02:13
Limiting the depth
to 3
truncates the result with ellipsis (...
). This can be useful if you’re looking at very deep or very complicated data.
02:23 Likewise, here’s a nifty little recursive piece of code.
02:32
print()
uses the ellipsis (...
) when recursion happens. pprint()
, on the other hand, will actually tell you it’s recursion.
02:40
So if you’re dealing with complicated lists that have recursive references inside of them, this can actually make it easier to debug. If you’ll notice, the id(items)
is the id
at the bottom of the recursive list there. One last thing to note though, is unlike print()
—that can take multiple arguments—pprint()
can only take a single object. If you pass in two strings, you will get an error, unlike in regular print()
.
03:08
So you’re stuck with one object at a time here, or composing the object in its own str()
call before you call pprint()
. A lot of terminals allow you to control the font color and weight.
03:22 I’ll show you how to use ANSI escape sequences in the next lesson.
Become a Member to join the conversation.