[Q] How to exec code object with local variables specified?

Discussion in 'Python' started by Makoto Kuwata, Sep 20, 2012.

  1. Hi,

    Is it possible to run code object with local variables specified?
    I'm trying the following code but not work:

    def fn():
    x = 1
    y = 2
    localvars = {'x': 0}
    exec(fn.func_code, globals(), localvars)
    print(localvars)
    ## what I expected is: {'x': 1, 'y': 2}
    ## but actual is: {'x': 0}

    Python: 2.7.3
    OS: MacOS X

    --
    regards,
    makoto kuwata
    Makoto Kuwata, Sep 20, 2012
    #1
    1. Advertising

  2. Makoto Kuwataæ–¼ 2012å¹´9月20日星期四UTC+8下åˆ7時27分40秒寫é“:
    > Hi,
    >
    >
    >
    > Is it possible to run code object with local variables specified?
    >
    > I'm trying the following code but not work:
    >
    >
    >
    > def fn():
    >
    > x = 1
    >
    > y = 2
    >
    > localvars = {'x': 0}
    >
    > exec(fn.func_code, globals(), localvars)
    >
    > print(localvars)
    >
    > ## what I expected is: {'x': 1, 'y': 2}
    >
    > ## but actual is: {'x': 0}
    >
    >
    >
    > Python: 2.7.3
    >
    > OS: MacOS X
    >
    >
    >
    > --
    >
    > regards,
    >
    > makoto kuwata


    Do you plan to write an IDE for python scrits ?

    I have seen a lot abusiosns of spawns or pipes of mutlti-processes,
    and now the exec part again.

    I think the sources of Doctor Python and IDLE were available
    years ago for showing off python's capabilities in developing an IDE.
    88888 Dihedral, Sep 21, 2012
    #2
    1. Advertising

  3. Makoto Kuwataæ–¼ 2012å¹´9月20日星期四UTC+8下åˆ7時27分40秒寫é“:
    > Hi,
    >
    >
    >
    > Is it possible to run code object with local variables specified?
    >
    > I'm trying the following code but not work:
    >
    >
    >
    > def fn():
    >
    > x = 1
    >
    > y = 2
    >
    > localvars = {'x': 0}
    >
    > exec(fn.func_code, globals(), localvars)
    >
    > print(localvars)
    >
    > ## what I expected is: {'x': 1, 'y': 2}
    >
    > ## but actual is: {'x': 0}
    >
    >
    >
    > Python: 2.7.3
    >
    > OS: MacOS X
    >
    >
    >
    > --
    >
    > regards,
    >
    > makoto kuwata


    Do you plan to write an IDE for python scrits ?

    I have seen a lot abusiosns of spawns or pipes of mutlti-processes,
    and now the exec part again.

    I think the sources of Doctor Python and IDLE were available
    years ago for showing off python's capabilities in developing an IDE.
    88888 Dihedral, Sep 21, 2012
    #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. Hal Vaughan
    Replies:
    11
    Views:
    1,104
    Gordon Beaton
    May 22, 2006
  2. Peter Otten
    Replies:
    0
    Views:
    146
    Peter Otten
    Sep 20, 2012
  3. Makoto Kuwata
    Replies:
    0
    Views:
    148
    Makoto Kuwata
    Sep 20, 2012
  4. Terry Reedy
    Replies:
    0
    Views:
    155
    Terry Reedy
    Sep 20, 2012
  5. Peter Otten
    Replies:
    0
    Views:
    146
    Peter Otten
    Sep 21, 2012
Loading...

Share This Page