exec "statement" VS. exec "statement" in globals(), locals()

Discussion in 'Python' started by tedsuzman, Jul 21, 2004.

  1. tedsuzman

    tedsuzman Guest

    -----
    def f():
    ret = 2
    exec "ret += 10"
    return ret

    print f()
    -----

    The above prints '12', as expected. However,

    ------
    def f():
    ret = 2
    exec "ret += 10" in globals(), locals()
    return ret

    print f()
    ------

    prints '2'. According to http://docs.python.org/ref/exec.html, "In all
    cases, if the optional parts are omitted, the code is executed in the
    current scope." Don't globals() and locals() consist of the current
    scope? Why aren't the two examples above equivalent?
     
    tedsuzman, Jul 21, 2004
    #1
    1. Advertising

  2. tedsuzman

    Larry Bates Guest

    From Python Manual:

    execfile(file[, globals[, locals]])
    This function is similar to the exec statement, but parses a file instead
    of a string. It is different from the import statement in that it does not
    use the module administration -- it reads the file unconditionally and does
    not create a new module.2.2
    The arguments are a file name and two optional dictionaries. The file is
    parsed and evaluated as a sequence of Python statements (similarly to a
    module) using the globals and locals dictionaries as global and local
    namespace. If the locals dictionary is omitted it defaults to the globals
    dictionary. If both dictionaries are omitted, the expression is executed in
    the environment where execfile() is called. The return value is None.

    Warning: The default locals act as described for function locals() below:
    modifications to the default locals dictionary should not be attempted. Pass
    an explicit locals dictionary if you need to see effects of the code on
    locals after function execfile() returns. execfile() cannot be used reliably
    to modify a function's locals.

    I think the Warning explains it.

    You can do:

    def f():
    l={'ret':2}
    exec("ret += 10", globals(), l)
    return l['ret']

    >>>print f()


    12

    "tedsuzman" <> wrote in message
    news:...

    > -----
    > def f():
    > ret = 2
    > exec "ret += 10"
    > return ret
    >
    > print f()
    > -----
    >
    > The above prints '12', as expected. However,
    >
    > ------
    > def f():
    > ret = 2
    > exec "ret += 10" in globals(), locals()
    > return ret
    >
    > print f()
    > ------
    >
    > prints '2'. According to http://docs.python.org/ref/exec.html, "In all
    > cases, if the optional parts are omitted, the code is executed in the
    > current scope." Don't globals() and locals() consist of the current
    > scope? Why aren't the two examples above equivalent?
    >
     
    Larry Bates, Jul 21, 2004
    #2
    1. Advertising

  3. Hi !


    In the syntax 2, because "ret=2", "ret" is local to f()




    Try :

    chaine="""global ret
    ret=ret+10"""

    def f():
    global ret
    ret = 2
    exec chaine in globals(), locals() #now, idem exec(chaine)
    return ret

    print f() #==> 12
     
    Michel Claveau, résurectionné d'outre-bombe inform, Jul 21, 2004
    #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. Giles Brown

    Question about "exec in globals, locals"

    Giles Brown, Jul 4, 2003, in forum: Python
    Replies:
    2
    Views:
    364
    Adrien Di Mascio
    Jul 4, 2003
  2. Ted
    Replies:
    1
    Views:
    475
    Duncan Booth
    Jul 22, 2004
  3. Olivier Sessink
    Replies:
    0
    Views:
    390
    Olivier Sessink
    Feb 24, 2005
  4. Faisal Alquaddoomi

    Globals, Locals Dictionaries + Callbacks Question

    Faisal Alquaddoomi, Dec 21, 2005, in forum: Python
    Replies:
    3
    Views:
    369
    Steve Holden
    Dec 22, 2005
  5. Replies:
    2
    Views:
    369
Loading...

Share This Page