What does it mean that a scope is determined statically and used dynamically?
<p>This is an excerpt of Python docs for Classes I'm struggling to understand:</p>

<blockquote>
  <p>A scope is a textual region of a Python program where a namespace is directly accessible. âDirectly accessibleâ here means that an unqualified reference to a name attempts to find the name in the namespace.</p>
  
  <p>Although scopes are determined statically, they are used dynamically.</p>
</blockquote>

<p>I didn't quite comprehend what the author meant by a scope from this definition, what's a textual region of a program, and what it means that scopes are statically determined and dynamically used. I have an intuitive understanding of a scope, but would love to fully appreciate the docs definition. If someone would be so kind as to elaborate what author had in mind it would be greatly appreciated.</p>

<h2>"Defined Statically"</h2>

<p>There is global scope and local scope (let's ignore the third one).</p>

<p>Whether a variable is global or local in some function is determined before the function is called, i.e. <strong>statically</strong>.</p>

<p>For example:</p>

<pre><code>a = 1
b = 2

def func1():
    c = 3

print func1.__code__.co_varnames  # prints ('c',)
</code></pre>

<p>It is determined statically that <code>func1</code> has one local variable and that its name is <code>c</code>. Statically, because it is done as soon as the function is created, not later when some local variable is actually accessed.</p>

<p>What are the consequences of that? Well, for example, this function fails:</p>

<pre><code>a = 1

def func2():
    print a   # raises an exception
    a = 2
</code></pre>

<p>If scopes were dynamic in Python, <code>func2</code> would have printed 1. Instead, in line with <code>print a</code> it is already known that <code>a</code> is a local variable, so the global <code>a</code> will not be used. Local <code>a</code> wont be used either, because it is not yet initialized.</p>

<h2>"Used Dynamically"</h2>

<p>From the <a href="https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces" rel="nofollow">same document</a>:</p>

<blockquote>
  <p>On the other hand, the actual search for names is done dynamically, at run time â however, the language definition is evolving towards static name resolution, at âcompileâ time, so donât rely on dynamic name resolution! (In fact, local variables are already determined statically.)</p>
</blockquote>

<p>Global variables are stored in a dictionary. When global variable <code>a</code> is accessed, the interpreter looks for key <code>a</code> in that dictionary. That is dynammic usage.</p>

<p>Local variables are not used that way. The interpreter knows beforehand how many variables a function has, so it can give each of them a fixed location. Then, accessing local variable <code>xy</code> can be optimized by simply taking <em>"the second local variable"</em> or <em>"the fifth local variable"</em>, without actually using the variable name.</p>

python class scope namespaces documentation