title: Python `collections.defaultdict` for the same class
body: <p>I try to use a Trie data structure for some coding problem.
  For each node in a trie, you typically put a a list of reference of its children.
  So, I thought about using defaultdict to create a default empty trie node if some children does not exists in a lookup.
  However, I don't know how to use defaultdict to refer to the class that enclose it.</p>
  <p>I tried two methods, which were both failed. The following is what I tried.</p>
  <pre class="lang-py prettyprint-override"><code>from dataclasses import dataclass
  from collections import defaultdict
  
  @dataclass   
  class TrieNode():
      is_word = False
      children = defaultdict(&quot;TrieNode&quot;)
  
  </code></pre>
  <p>The code above produce</p>
  <pre><code>Traceback (most recent call last):
    File &quot;&lt;stdin&gt;&quot;, line 2, in &lt;module&gt;
    File &quot;&lt;stdin&gt;&quot;, line 4, in TrieNode
  TypeError: first argument must be callable or None
  </code></pre>
  <pre class="lang-py prettyprint-override"><code>@dataclass   
  class TrieNode():
      is_word = False
      children = defaultdict(TrieNode)
  </code></pre>
  <p>The above will produce</p>
  <pre><code>Traceback (most recent call last):
    File &quot;&lt;stdin&gt;&quot;, line 2, in &lt;module&gt;
    File &quot;&lt;stdin&gt;&quot;, line 4, in TrieNode
  NameError: name 'TrieNode' is not defined
  </code></pre>
  <p>My question is about how do you use <code>defaultdict</code> to implement this elegantly.
  Thank you very much in advance.</p>