title: Getting "global name 'foo' is not defined" with Python's timeit
body: <p>I'm trying to find out how much time it takes to execute a Python statement, so I looked online and found that the standard library provides a module called <a href="http://docs.python.org/library/timeit.html" rel="noreferrer">timeit</a> that purports to do exactly that:</p>
  
  <pre><code>import timeit
  
  def foo():
      # ... contains code I want to time ...
  
  def dotime():
      t = timeit.Timer("foo()")
      time = t.timeit(1)
      print "took %fs\n" % (time,)
  
  dotime()
  </code></pre>
  
  <p>However, this produces an error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "&lt;stdin&gt;", line 3, in dotime
    File "/usr/local/lib/python2.6/timeit.py", line 193, in timeit
      timing = self.inner(it, self.timer)
    File "&lt;timeit-src&gt;", line 6, in inner
  NameError: global name 'foo' is not defined
  </code></pre>
  
  <p>I'm still new to Python and I don't fully understand all the scoping issues it has, but I don't know why this snippet doesn't work. Any thoughts?</p>