Question about "exec in globals, locals"

Discussion in 'Python' started by Giles Brown, Jul 4, 2003.

  1. Giles Brown

    Giles Brown Guest

    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>
     
    Giles Brown, Jul 4, 2003
    #1
    1. Advertising

  2. Giles Brown

    Egor Bolonev Guest

    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()

    With best regards, Egor Bolonev. E-mail: [ru eo en]
     
    Egor Bolonev, Jul 4, 2003
    #2
    1. Advertising

  3. 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.

    Cheers,

    --
    Adrien Di Mascio
    LOGILAB, Paris (France).
    http://www.logilab.com http://www.logilab.fr http://www.logilab.org
     
    Adrien Di Mascio, Jul 4, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sean
    Replies:
    1
    Views:
    628
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    697
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    975
    Scott Allen
    Oct 6, 2005
  4. Philip Meyer
    Replies:
    0
    Views:
    448
    Philip Meyer
    Nov 30, 2003
  5. Bit Byte
    Replies:
    1
    Views:
    883
    Teemu Keiski
    Jan 28, 2007
Loading...

Share This Page