Question about "exec in globals, locals" 
Author Message
 Question about "exec in globals, locals"

I do not understand why the following code produces
    NameError: name 'FirstClass' is not defined
when both a global and local dict are passed into exec, but
not when only a global dict is passed in.

I seek enlightenment!

Thanks,
Giles Brown

<python_code>

source = """
class FirstClass:
    pass
class SecondClass:
    References = [FirstClass]
"""

# Case - 1
myglobals = {'__builtins__' : None, '__name__': None}
exec source in myglobals

print "Global names are:", myglobals.keys()

# Case - 2
myglobals = {'__builtins__' : None, '__name__': None}
mylocals = {}

exec source in myglobals, mylocals

print "Global names are:", myglobals.keys()
print "Local names are:", mylocals.keys()

</python_code>



Tue, 20 Dec 2005 16:41:36 GMT  
 Question about "exec in globals, locals"
Hello, Giles!
You wrote  on 4 Jul 2003 01:41:36 -0700:

[Snipped]

GB> # Case - 2
GB> myglobals = {'__builtins__' : None, '__name__': None}
GB> mylocals = {}

GB> exec source in myglobals, mylocals
exec source in mylocals
exec source in myglobals
And it work with no errors. But I'm not sure you wish this.

GB> print "Global names are:", myglobals.keys()
GB> print "Local names are:", mylocals.keys()




Tue, 20 Dec 2005 17:48:35 GMT  
 Question about "exec in globals, locals"
Hi,

Quote:
> source = """
> class FirstClass:
>     pass
> class SecondClass:
>     References = [FirstClass]
> """

When you specify both locals() and globals(), here's what happen :

"""
class FirstClass:
    pass

## ---------> Here, your locals() dict have been updated, but not your
## globals() one.
## But, here, you can do : References = [FirstClass] since your
## locals()
## know about 'FirstClass'

class SecondClass:
    ## ---------> Here, your locals() dict is a new one since you've
    just
    ## entered a new scope. So, 'FirstClass' is neither defined in
    ## 'locals()' nor in 'globals()', that's why you have your
    NameError
"""

I'm not quite sure of my explanation, but that could explain your
problem. Sorry if I've made any python mistake.

Cheers,

--
Adrien Di Mascio
LOGILAB, Paris (France).
http://www.logilab.com   http://www.logilab.fr   http://www.logilab.org



Tue, 20 Dec 2005 18:51:56 GMT  
 Question about "exec in globals, locals"

Quote:
> Hi,
> > source = """
> > class FirstClass:
> >     pass
> > class SecondClass:
> >     References = [FirstClass]
> > """

> When you specify both locals() and globals(), here's what happen :

> """
> class FirstClass:
>     pass

> ## ---------> Here, your locals() dict have been updated, but not your
> ## globals() one.
> ## But, here, you can do : References = [FirstClass] since your
> ## locals()
> ## know about 'FirstClass'

> class SecondClass:
>     ## ---------> Here, your locals() dict is a new one since you've
>     just
>     ## entered a new scope. So, 'FirstClass' is neither defined in
>     ## 'locals()' nor in 'globals()', that's why you have your
>     NameError
> """

> I'm not quite sure of my explanation, but that could explain your
> problem. Sorry if I've made any Python mistake.

Hmmm.  You might be right, but wouldn't you expect the scope of SecondClass to
be nested within the scope of the passed in locals()?

I find this quite confusing, which unusual for a Python ;-)

Thanks,
Giles Brown



Thu, 22 Dec 2005 02:41:27 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Question about "SYSTEM EXEC" vi

2. "exec" Question

3. Question about "exec"

4. Questions/problems with Tcl "exec" command

5. TCL "info globals" problem

6. "By-process" globals in VisualAge

7. beginners "let"/"random" question

8. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

9. Witness Lee and the "Local Churches": A Personal Testimony

10. Clipper Debugger "loses" Local variables

11. "Local" Batch Files

12. "local l : like Result"

 

 
Powered by phpBB® Forum Software