title: How to write the Visitor Pattern for Abstract Syntax Tree in Python?
body: <p>My collegue suggested me to write a visitor pattern to navigate the AST. Can anyone tell me more how would I start writing it?</p>
  
  <p>As far as I understand, each Node in AST would have <code>visit()</code> method (?) that would somehow get called (from where?). That about concludes my understanding.</p>
  
  <p>To simplify everything, suppose I have nodes <code>Root</code>, <code>Expression</code>, <code>Number</code>, <code>Op</code> and the tree looks like this:</p>
  
  <pre><code>       Root
          |
         Op(+)
        /   \
       /     \
   Number(5)  \
               Op(*)
               /   \
              /     \
             /       \
         Number(2)   Number(444)
  </code></pre>
  
  <p>Can anyone think of how the visitor pattern would visit this tree to produce output:</p>
  
  <pre><code> 5 + 2 * 444
  </code></pre>
  
  <p>Thanks, Boda Cydo.</p>