Jive Dadson wrote:
Yeah. I got it.
exec "foo = 555" in globals(), locals() does the trick.
You can do it with your own dicts, too -- but they must already exist,
exec doesn't create them out of nowhere.
>>> myglobals = {'a':2, 'b':5}
>>> mylocals = {'c': 3}
>>> exec "d = a * b + c" in myglobals,
Yeah. I got it.
exec "foo = 555" in globals(), locals() does the trick.
--
http://mail.python.org/mailman/listinfo/python-list
Jeff Shannon wrote:
>
> Jive Dadson wrote:
>
> > How does one execute arbitrary text as code within a module's context?
> >
> > I've got some code that compiles some text and then executes it. When
> > the string is "print 'Hello'", it prints "Hello". I get no exception
> > when I compile and
Jive Dadson wrote:
I've got some code that compiles some text and then executes it. When
the string is "print 'Hello'", it prints "Hello". I get no exception
when I compile and execute "foo = 555". If I then compile and exec
"print foo", I get a name error. The variable foo is undefined. My
assu
Jive Dadson wrote:
How does one execute arbitrary text as code within a module's context?
I've got some code that compiles some text and then executes it. When
the string is "print 'Hello'", it prints "Hello". I get no exception
when I compile and execute "foo = 555". If I then compile and exec
How does one execute arbitrary text as code within a module's context?
I've got some code that compiles some text and then executes it. When
the string is "print 'Hello'", it prints "Hello". I get no exception
when I compile and execute "foo = 555". If I then compile and exec
"print foo", I get