Note that there are some explanatory texts on larger screens.

plurals
  1. POglobals and locals in python exec()
    primarykey
    data
    text
    <p>I'm trying to run a piece of python code using exec.</p> <pre><code>my_code = """ class A(object): pass print 'locals: %s' % locals() print 'A: %s' % A class B(object): a_ref = A """ global_env = {} local_env = {} my_code_AST = compile(my_code, "My Code", "exec") exec(my_code_AST, global_env, local_env) print local_env </code></pre> <p>which results in the following output</p> <pre><code>locals: {'A': &lt;class 'A'&gt;} A: &lt;class 'A'&gt; Traceback (most recent call last): File "python_test.py", line 16, in &lt;module&gt; exec(my_code_AST, global_env, local_env) File "My Code", line 8, in &lt;module&gt; File "My Code", line 9, in B NameError: name 'A' is not defined </code></pre> <p>However, if I change the code to this -</p> <pre><code>my_code = """ class A(object): pass print 'locals: %s' % locals() print 'A: %s' % A class B(A): pass """ global_env = {} local_env = {} my_code_AST = compile(my_code, "My Code", "exec") exec(my_code_AST, global_env, local_env) print local_env </code></pre> <p>then it works fine - giving the following output -</p> <pre><code>locals: {'A': &lt;class 'A'&gt;} A: &lt;class 'A'&gt; {'A': &lt;class 'A'&gt;, 'B': &lt;class 'B'&gt;} </code></pre> <p>Clearly A is present and accessible - what's going wrong in the first piece of code? I'm using 2.6.5, cheers,</p> <p>Colin</p> <p><strong>* UPDATE 1 *</strong></p> <p>If I check the locals() inside the class -</p> <pre><code>my_code = """ class A(object): pass print 'locals: %s' % locals() print 'A: %s' % A class B(object): print locals() a_ref = A """ global_env = {} local_env = {} my_code_AST = compile(my_code, "My Code", "exec") exec(my_code_AST, global_env, local_env) print local_env </code></pre> <p>Then it becomes clear that locals() is not the same in both places -</p> <pre><code>locals: {'A': &lt;class 'A'&gt;} A: &lt;class 'A'&gt; {'__module__': '__builtin__'} Traceback (most recent call last): File "python_test.py", line 16, in &lt;module&gt; exec(my_code_AST, global_env, local_env) File "My Code", line 8, in &lt;module&gt; File "My Code", line 10, in B NameError: name 'A' is not defined </code></pre> <p>However, if I do this, there is no problem -</p> <pre><code>def f(): class A(object): pass class B(object): a_ref = A f() print 'Finished OK' </code></pre> <p><strong>* UPDATE 2 *</strong></p> <p>ok, so the docs here - <a href="http://docs.python.org/reference/executionmodel.html" rel="noreferrer">http://docs.python.org/reference/executionmodel.html</a></p> <p><i>'A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution. The namespace of the class definition becomes the attribute dictionary of the class. Names defined at the class scope are not visible in methods.'</i></p> <p>It seems to me that 'A' should be made available as a free variable within the executable statement that is the definition of B, and this happens when we call f() above, but not when we use exec(). This can be more easily shown with the following -</p> <pre><code>my_code = """ class A(object): pass print 'locals in body: %s' % locals() print 'A: %s' % A def f(): print 'A in f: %s' % A f() class B(object): a_ref = A """ </code></pre> <p>which outputs</p> <pre><code>locals in body: {'A': &lt;class 'A'&gt;} A: &lt;class 'A'&gt; Traceback (most recent call last): File "python_test.py", line 20, in &lt;module&gt; exec(my_code_AST, global_env, local_env) File "My Code", line 11, in &lt;module&gt; File "My Code", line 9, in f NameError: global name 'A' is not defined </code></pre> <p>So I guess the new question is - why aren't those locals being exposed as free variables in functions and class definitions - it seems like a pretty standard closure scenario.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload