Note: This question is about the mechanics of pprint and not asking about namedtuples. Namedtuples are used as a tangible example, but the question is not "how do I make namedtuple more beautiful for pprinting?"
What method does pprint
call to display an object's data?
How can I override this?
In this example, I would like to pretty print the named tuple d
so that it prints one data element per line, similar to how the dict a
is being printed.
>>> import pprint
>>> import collections
>>> a={'a': 'a'*40, 'b': 'b'*40}
>>> d=collections.namedtuple('D', 'a b')('a'*40, 'b'*40)
>>> # How do I make d print similarly to a?
>>> pprint.pprint(a)
{'a': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'b': 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'}
>>> pprint.pprint(d)
D(a='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', b='bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb')