title: Why is locality determined at compile time?
body: <p>This is a bit of a follow-up to <a href="https://stackoverflow.com/questions/19063648/python-closures/19063658#19063658">this question</a>.</p>
  
  <ol>
  <li>Why is locality determined at compile time and not at execution time? Is it purely for performance?</li>
  <li>Are there languages that look up their variables at execution time? I.e. every time a variable is accessed, this variable is first searched in the local scope and then the search escalates through all enclosing scopes?</li>
  <li>How do ECMA languages handle this?</li>
  </ol>
  
  <p>To put question 2 in other words: Are there languages where the following code (in the necessary syntax) works:</p>
  
  <pre><code>def f(): print ('f')
  
  def g():
      f()
      f = 42
  
  g()
  </code></pre>