title: clojure and ^:dynamic
body: <p>I tried to understand dynamic variables and binding function so I tried this (clojure 1.3):</p>
  
  <pre><code>user=&gt; (defn f [] 
             (def ^:dynamic x 5) 
             (defn g [] (println x)) 
             (defn h [] (binding [x 3] (g))) 
             (h))
  #'user/f
  user=&gt; (f)     
  5
  nil
  </code></pre>
  
  <p>Confused, I tried this somewhat simpler code:</p>
  
  <pre><code>user=&gt; (def ^:dynamic y 5)
  #'user/y
  user=&gt; (defn g [] (println y))
  #'user/g
  user=&gt; (defn h [] (binding [y 3] (g)))
  #'user/h
  user=&gt; (h)
  3
  nil
  </code></pre>
  
  <p>What is the difference between the two pieces of code? Why does the second example work but the first does not?</p>
  
  <p>Hint: I just realized that the following works (still don't fully understand why):</p>
  
  <pre><code>user=&gt; (def ^:dynamic y 5)
  #'user/y
  user=&gt; (defn f [] (defn g [] (println y)) (defn h [] (binding [y 3] (g))) (h))
  #'user/f
  user=&gt; (f)
  3
  nil
  user=&gt; 
  </code></pre>