Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [Q] How to exec code object with local variables specified?

Reply
Thread Tools

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

 
 
Makoto Kuwata
Guest
Posts: n/a
 
      09-20-2012
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
 
Reply With Quote
 
 
 
 
88888 Dihedral
Guest
Posts: n/a
 
      09-21-2012
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.
 
Reply With Quote
 
 
 
 
88888 Dihedral
Guest
Posts: n/a
 
      09-21-2012
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: [Q] How to exec code object with local variables specified? Peter Otten Python 0 09-21-2012 07:42 AM
Re: [Q] How to exec code object with local variables specified? Terry Reedy Python 0 09-20-2012 04:18 PM
Re: [Q] How to exec code object with local variables specified? Makoto Kuwata Python 0 09-20-2012 03:40 PM
Re: [Q] How to exec code object with local variables specified? Peter Otten Python 0 09-20-2012 01:15 PM



Advertisments