Re: embedding python in python

From: Maurice LING (mauriceling_at_acm.org)
Date: 10/01/04


Date: Thu, 30 Sep 2004 23:09:16 GMT

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



Relevant Pages

  • Re: how do you get the name of a dictionary?
    ... exception that says "a is too short", you often have to mentally change ... a is set in the enclosing scope, it makes sense to refer to "the object ... You don't have to mentally change gears; ... Here's a traceback. ...
    (comp.lang.python)
  • Re: how do you get the name of a dictionary?
    ... x could be any one of a, b, c or d, and the traceback ... and reductio ad absurdum fallacies doesn't invalidate my point that there ... the object was created in a different scope with a different name. ... those valid uses aren't worth the cost of implementing them. ...
    (comp.lang.python)
  • Re: Question about if in Ruby
    ... >> It does assign it if it already exists in the current scope. ... >> well in ruby. ... > NameError: undefined local variable or method `a' for main:Object ... > NameError after the assignment if the variable didn't exist before. ...
    (comp.lang.ruby)
  • Re: Question about if in Ruby
    ... This is not exactly true (at least on ruby 1.8.6): ... NameError: undefined local variable or method `b' for main:Object ... It does assign it if it already exists in the current scope. ... NameError after the assignment if the variable didn't exist before. ...
    (comp.lang.ruby)
  • Re: reference counting and file objects
    ... fp gets closed when it leaves scope. ... One issue is that when the function terminates through an exception, ... the file stays open as long as the traceback is available, ...
    (comp.lang.python)