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


Groups > comp.lang.python > #29556 > unrolled thread

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

Started byTerry Reedy <tjreedy@udel.edu>
First post2012-09-20 12:18 -0400
Last post2012-09-20 12:18 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: [Q] How to exec code object with local variables specified? Terry Reedy <tjreedy@udel.edu> - 2012-09-20 12:18 -0400

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

FromTerry Reedy <tjreedy@udel.edu>
Date2012-09-20 12:18 -0400
SubjectRe: [Q] How to exec code object with local variables specified?
Message-ID<mailman.962.1348157899.27098.python-list@python.org>
On 9/20/2012 7:27 AM, Makoto Kuwata wrote:

> Is it possible to run code object with local variables specified?

In the way you mean that, no.

> I'm trying the following code but not work:
>
>      def fn():
>         x = 1
>         y = 2
>      localvars = {'x': 0}
>      exec(fn.func_code, globals(), localvars)

The globals and locals you pass to exec are the globals and locals for 
the context in which the code object runs. They have nothing to do with 
the code objects local namespace.

Running exec with separate globals and locals is like running the code 
within a class definition context. If you ran

def fn(): x = 1
class dummy:
   fn()

dummy.x would not be defined and I presume you would not expect it to.

-- 
Terry Jan Reedy

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web