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>There's no way to know what, if anything, the author(s) were thinking or even whether they gave it much consideration at all.</p>

<p>For the specific case of nested <code>defaultdict</code>s, as shown your example code:</p>

<pre><code>def factory():
    return defaultdict(factory)
nested_dd = defaultdict(factory)
nested_dd['key1']['key2']['key3'] = 'value'
</code></pre>

<p>You can avoid the issue by subclassing <code>dict</code> like this instead:</p>

<pre><code>class Tree(dict):
    def __missing__(self, key):
        value = self[key] = type(self)()
        return value

nested_dd = Tree()
nested_dd['key1']['key2']['key3'] = 'value'
print(nested_dd) # -&gt; {'key1': {'key2': {'key3': 'value'}}}
</code></pre>

<p>Since the subclass doesn't define its own <code>__repr__()</code> or <code>__str__()</code> methods, instances of it will <code>print</code> (and <code>pprint</code>) just like regular <code>dict</code> instances do.</p>

python