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

M

Makoto Kuwata

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
 
8

88888 Dihedral

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.
 
8

88888 Dihedral

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,730
Latest member
AudryNolan

Latest Threads

Top