title: Python: Modifying pprint behavior for a class or object?
body: <p><strong>Note:</strong> 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 &quot;how do I make namedtuple more beautiful for pprinting?&quot;</p>
  <hr />
  <p>What method does <code>pprint</code> call to display an object's data?
  How can I override this?</p>
  <p>In this example, I would like to pretty print the named tuple <code>d</code> so that it prints one data element per line, similar to how the dict <code>a</code> is being printed.</p>
  <pre><code>&gt;&gt;&gt; import pprint
  &gt;&gt;&gt; import collections
  
  &gt;&gt;&gt; a={'a': 'a'*40, 'b': 'b'*40}
  &gt;&gt;&gt; d=collections.namedtuple('D', 'a b')('a'*40, 'b'*40)
  
  &gt;&gt;&gt; # How do I make d print similarly to a?
  &gt;&gt;&gt; pprint.pprint(a)
  {'a': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
   'b': 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'}
  &gt;&gt;&gt; pprint.pprint(d)
  D(a='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', b='bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb')
  </code></pre>