title: Is it possible to detect the number of local variables declared in a function?
body: <p>In a Python test fixture, is it possible to count how many local variables a function declares in its body?</p>
  
  <pre><code>def foo():
      a = 1
      b = 2
  
  Test.assertEqual(countLocals(foo), 2)
  </code></pre>
  
  <p>Alternatively, is there a way to see if a function declares any variables at all?</p>
  
  <pre><code>def foo():
      a = 1
      b = 2
  
  def bar():
      pass
  
  Test.assertEqual(hasLocals(foo), True)
  Test.assertEqual(hasLocals(bar), False)
  </code></pre>
  
  <p>The use case I'm thinking of has to do with validating user-submitted code.</p>