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

Discussion in 'Python' started by Peter Otten, Sep 21, 2012.

  1. Peter Otten

    Peter Otten Guest

    Makoto Kuwata wrote:

    > On Thu, Sep 20, 2012 at 10:15 PM, Peter Otten <> wrote:
    >>
    >>>>> loc = {}
    >>>>> exec("x = 1; y = 2", globals(), loc)
    >>>>> loc

    >> {'y': 2, 'x': 1}
    >>
    >> However, this won't work with the code object taken from a function which
    >> uses a different a bytecode (STORE_FAST instead of STORE_NAME):
    >>

    >
    > Is there any way to use STORE_FAST instead of STORE_NAME?
    >
    > exec("string", ...) is not a solution for me.


    Can you describe your use-case? Perhaps we can suggest an alternative
    approach.

    > # What is different between fn.func_code and compile("string")?


    func_code has faster bytecode at the expense of flexibility.
     
    Peter Otten, Sep 21, 2012
    #1
    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,195
    Gordon Beaton
    May 22, 2006
  2. Makoto Kuwata
    Replies:
    2
    Views:
    183
    88888 Dihedral
    Sep 21, 2012
  3. Peter Otten
    Replies:
    0
    Views:
    154
    Peter Otten
    Sep 20, 2012
  4. Makoto Kuwata
    Replies:
    0
    Views:
    162
    Makoto Kuwata
    Sep 20, 2012
  5. Terry Reedy
    Replies:
    0
    Views:
    164
    Terry Reedy
    Sep 20, 2012
Loading...

Share This Page