embedding python in python

Maurice LING mauriceling at acm.org
Fri Oct 1 01:09:16 CEST 2004


Hi,

Sorry, I have another problem here. Given this snipplet,

 >>> def b(s):
...     exec(s)
...     exec('print "x= " + str(x)')
...
 >>> b('x = 10')
x= 10
 >>>
 >>> print x
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
NameError: name 'x' is not defined
 >>>

Clearly, all the objects that were declared using exec() in function b 
went out of scope, is there anyway to prevent this from happening?

What I really need is this, for example, in a class,

1. a function to initialize a set of objects
2. a function which carries commands to act on the object (unknown at 
compile time)
3. a function to read the values of the set of objects

So, if the objects went out of scope after leaving (1), then I am rather 
screwed when I'm in (2). Any remedies?

Thanks
Maurice



More information about the Python-list mailing list