Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29603

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

From Peter Otten <__peter__@web.de>
Subject Re: [Q] How to exec code object with local variables specified?
Date 2012-09-21 09:42 +0200
Organization None
References <CAFTm5Ru=E8Zii4k54fc4ii43wKPSwTq=iT4iRQCWy=46YiEZZw@mail.gmail.com> <k3f4ri$m30$1@ger.gmane.org> <CAFTm5RvWXwH-mtMftj6X8T9-Pz1_i3tAc10td5==vVO54+ENpQ@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.989.1348213304.27098.python-list@python.org> (permalink)

Show all headers | View raw


Makoto Kuwata wrote:

> On Thu, Sep 20, 2012 at 10:15 PM, Peter Otten <__peter__@web.de> 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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: [Q] How to exec code object with local variables specified? Peter Otten <__peter__@web.de> - 2012-09-21 09:42 +0200

csiph-web