func_code vs. string problem
Filip Dreger
fdreger at amiga.pl
Sat Apr 23 16:19:48 EDT 2005
I came up with a simpler description of the problem.
It's all in the simple source:
# we define 'print b' in three different ways: as a string,
# a bytecode and a function
string="print b"
code=compile(string,'string','exec')
def function():
print b
# now we make functions that test if it is possible to execute 'print
b'
# in some local scope
def execstring():
b=5
exec string
def execfunction():
b=5
exec function.func_code
def execcode():
b=5
exec code
execstring() # works
execcode() # works
execfunction() # throws name error exception...
My problem is that both 'string' and 'code' are references to code
objects, so they _should_ behave in the same way... I am close to
giving up...
I am trying to find a way of executing functions without creating a
nested scope, so they can share local and global namespace (even if
they are declared in some other module). I _could_ turn them into
strings and pass around as compiled objects, butthis would be very
ugly. I am sure Python has some better, cleaner way to do this.
regards,
Filip Dreger
More information about the Python-list
mailing list