exec "def.." in globals(), locals() does not work

Discussion in 'Python' started by xml0x1a@yahoo.com, Feb 20, 2007.

  1. Guest

    How do I use exec?
    >python -V

    Python 2.4.3

    ----
    from math import *
    G = 1
    def d():
    L = 1
    exec "def f(x): return L + log(G) " in globals(), locals()
    f(1)
    ----

    How do I use exec() such that:
    1. A function defined in exec is available to the local scope (after
    exec returns)
    2. The defined function f has access to globals (G and log(x) from
    math)
    3. The defined function f has access to locals (L)

    So far I have only been able to get 2 out of the 3 requirements.
    It seems that exec "..." in locals(), globals() only uses the first
    listed scope.

    Bottomline:
    exec "..." in globals(), locals(), gets me 1. and 3.
    exec "..." in locals(), globals() gets me 1. and 2.
    exec "..." in hand-merged copy of the globals and locals dictionaries
    gets me 2. and 3.

    How do I get 1. 2. and 3.?

    Thanks in advance
     
    , Feb 20, 2007
    #1
    1. Advertising

  2. On Feb 19, 10:52 pm, wrote:
    > How do I use exec?


    Before you ask this question, the one you should have an answer for is
    "why do I (think I) have to use exec ?". At least for the example you
    gave, you don't; Python supports local functions and nested scopes,
    with no need for exec:

    from math import *
    G = 1

    def d():
    L = 1
    def f(x):
    return L + log(G)
    return f(1)


    >python -V
    > Python 2.4.3
    >
    > ----
    > from math import *
    > G = 1
    > def d():
    > L = 1
    > exec "def f(x): return L + log(G) " in globals(), locals()
    > f(1)
    > ----
    >
    > How do I use exec() such that:
    > 1. A function defined in exec is available to the local scope (after
    > exec returns)
    > 2. The defined function f has access to globals (G and log(x) from
    > math)
    > 3. The defined function f has access to locals (L)
    >
    > So far I have only been able to get 2 out of the 3 requirements.
    > It seems that exec "..." in locals(), globals() only uses the first
    > listed scope.
    >
    > Bottomline:
    > exec "..." in globals(), locals(), gets me 1. and 3.
    > exec "..." in locals(), globals() gets me 1. and 2.
    > exec "..." in hand-merged copy of the globals and locals dictionaries
    > gets me 2. and 3.
    >
    > How do I get 1. 2. and 3.?


    L is local in d() only. As far as f() is concerned, L,G and log are
    all globals, only x is local (which you don't use; is this a typo?).
    If you insist on using exec (which, again, you have no reason to for
    this example), take the union of d's globals and locals as f's
    globals, and store f in d's locals():

    from math import *
    G = 1

    def d():
    L = 1
    g = dict(globals())
    g.update(locals())
    exec "def f(x): return L + log(G) " in g, locals()
    return f(1)


    George
     
    George Sakkis, Feb 20, 2007
    #2
    1. Advertising

  3. Guest

    On Feb 19, 8:15 pm, "George Sakkis" <> wrote:
    > On Feb 19, 10:52 pm, wrote:
    >
    > > How do I use exec?

    >
    > Before you ask this question, the one you should have an answer for is
    > "why do I (think I) have to use exec ?". At least for the example you
    > gave, you don't; Python supports local functions and nested scopes,
    > with no need for exec:
    >
    > from math import *
    > G = 1
    >
    > def d():
    > L = 1
    > def f(x):
    > return L + log(G)
    > return f(1)
    >
    >
    >
    > >python -V
    > > Python 2.4.3

    >
    > > ----
    > > from math import *
    > > G = 1
    > > def d():
    > > L = 1
    > > exec "def f(x): return L + log(G) " in globals(), locals()
    > > f(1)
    > > ----

    >
    > > How do I use exec() such that:
    > > 1. A function defined in exec is available to the local scope (after
    > > exec returns)
    > > 2. The defined function f has access to globals (G and log(x) from
    > > math)
    > > 3. The defined function f has access to locals (L)

    >
    > > So far I have only been able to get 2 out of the 3 requirements.
    > > It seems that exec "..." in locals(), globals() only uses the first
    > > listed scope.

    >
    > > Bottomline:
    > > exec "..." in globals(), locals(), gets me 1. and 3.
    > > exec "..." in locals(), globals() gets me 1. and 2.
    > > exec "..." in hand-merged copy of the globals and locals dictionaries
    > > gets me 2. and 3.

    >
    > > How do I get 1. 2. and 3.?

    >
    > L is local in d() only. As far as f() is concerned, L,G and log are
    > all globals, only x is local (which you don't use; is this a typo?).
    > If you insist on using exec (which, again, you have no reason to for
    > this example), take the union of d's globals and locals as f's
    > globals, and store f in d's locals():
    >


    Thanks! this works.
    I tried to use g to merge locals and globals
    But I didn't think of keeping the locals() so that f is visible in d.
     
    , Feb 20, 2007
    #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:
    361
    Adrien Di Mascio
    Jul 4, 2003
  2. tedsuzman
    Replies:
    2
    Views:
    7,094
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  3. Ted
    Replies:
    1
    Views:
    470
    Duncan Booth
    Jul 22, 2004
  4. Olivier Sessink
    Replies:
    0
    Views:
    389
    Olivier Sessink
    Feb 24, 2005
  5. Faisal Alquaddoomi

    Globals, Locals Dictionaries + Callbacks Question

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

Share This Page