Is printing defaultdict supposed to be ugly (non human-readable) by default?
<p>Print <code>dict</code> and <code>defaultdict</code>:</p>

<pre><code>&gt;&gt;&gt; d = {'key': 'value'}
&gt;&gt;&gt; print(d)
{'key': 'value'}

&gt;&gt;&gt; dd = defaultdict(lambda: 'value')
&gt;&gt;&gt; dd['key']
'value'
&gt;&gt;&gt; print(dd)
defaultdict(&lt;function &lt;lambda&gt; at 0x7fbd44cb6b70&gt;, {'key': 'value'})
</code></pre>

<p>With nested structure it becomes ugly:</p>

<pre><code>&gt;&gt;&gt; nested_d = {'key1': {'key2': {'key3': 'value'}}}
&gt;&gt;&gt; print(nested_d)
{'key1': {'key2': {'key3': 'value'}}}

&gt;&gt;&gt; def factory():
...     return defaultdict(factory)
... 
&gt;&gt;&gt; nested_dd = defaultdict(factory)
&gt;&gt;&gt; nested_dd['key1']['key2']['key3'] = 'value'
&gt;&gt;&gt; print(nested_dd)
defaultdict(&lt;function factory at 0x7fbd44cd4ea0&gt;, {'key1': defaultdict(&lt;function factory at 0x7fbd44cd4ea0&gt;, {'key2': defaultdict(&lt;function factory at 0x7fbd44cd4ea0&gt;, {'key3': 'value'})})})
</code></pre>

<p>Were there any reasons for not making it human-readable by default? (UPD: I mean what are the reasons behind not having custom <code>__str__</code> defined for <code>defaultdict</code> by default?)</p>

<p><code>repr()</code> output (<code>defaultdict</code> has no <code>__str__</code>, only <code>__repr__</code>) is  <em>debugging output</em>. It is not meant to be pretty, it is meant to be <em>functional</em>. It tells you the type, the <code>repr()</code> of the callable that produces the default, and the contents.</p>

<p>From the <a href="https://docs.python.org/3/reference/datamodel.html#object.__repr__" rel="nofollow"><code>__repr__</code> documentation</a>:</p>

<blockquote>
  <p>This is typically used for debugging, so it is important that the representation is information-rich and unambiguous.</p>
</blockquote>

<p>Like all datatypes in Python, (except for strings for obvious reasons), no informal (<code>__str__</code>) is defined because it is up to the programmer to decide what output is suitable for their use-cases. No default can be set for that, because use-cases vary so widely. Output for a file has different needs than output to a GUI or to a web-page for example.</p>

<p>In Python 2, convert the object to a plain dictionary first, then use <code>pprint()</code> if you want 'pretty' output:</p>

<pre><code>def todict(d):
    if not isinstance(d, dict):
        return d
    return {k: todict(v) for k, v in d.items()}

pprint(todict(nested_dd))
</code></pre>

<p>In Python 3, <code>pprint</code> supports <code>defaultdict</code> directly:</p>

<pre><code>&gt;&gt;&gt; pprint(nested_dd)
defaultdict(&lt;function factory at 0x105ed2f28&gt;,
            {'key1': defaultdict(&lt;function factory at 0x105ed2f28&gt;,
                                 {'key2': defaultdict(&lt;function factory at 0x105ed2f28&gt;,
                                                      {'key3': 'value'})})})
</code></pre>

python