title: what is the difference between eval and exec in python?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/2220699/whats-the-difference-between-eval-exec-and-compile-in-python">What’s the difference between eval, exec, and compile in Python?</a>  </p>
  </blockquote>
  
  
  
  <p>I known that</p>
  
  <ol>
  <li>eval is a <code>function</code></li>
  <li>exec is a <code>statement</code></li>
  </ol>
  
  <p>And the simple usage of both is :</p>
  
  <pre><code>eval('1+2')
  exec 'print 1+2'
  </code></pre>
  
  <p>But there are other usages that I can't understand.</p>
  
  <ol>
  <li><p>Using a variable to store a function name, and using this variable to
  call the function<br>
  eg:</p>
  
  <pre><code>def test():
      print 'hello world'
  func = 'test'
  func = eval(func)
  func() # this will call test()
  </code></pre>
  
  <p>I type(func) after<br>
  <code>func = eval(func)</code><br>
  it returns<br>
  <code>&lt;type 'function'&gt;</code><br>
  I read the document of <code>eval</code>, but I don't known why the eval can do
  this.</p></li>
  <li><p>Using a variable to store a module name, and using this variable to import the module.<br>
  eg.</p>
  
  <pre><code>m = 'sys'
  exec "import " +　m
  </code></pre>
  
  <p>Is this the reason:<br>
  <code>import module_name</code> is a statement, not expression?<br>
  and:<br>
  <code>eval</code> does only to calculate a expression<br>
  <code>exec</code> does to run the statement in the str?</p></li>
  </ol>