On Fri, Nov 23, 2012 at 11:14 PM, <esan...@gmail.com> wrote: > Why isn't 'a' defined? > Shouldn't you be able to define the global variables with a dict passed to > eval? > Is there an other way to do this, beside the two obvious: defining 'a' before > calling gp_function and using a as an argument in gp_function?
It is defined, but not in the context of the called function. You defined that function in a particular scope, which then becomes its global scope. (I'm handwaving a lot of details here. Bear with me.) When you eval a bit of code, you define the global scope for _that code_, but not what it calls. Calling gp_function from inside there switches to the new global scope and off it goes. Normally, I'd recommend your second option, passing a as an argument. It's flexible, clear, doesn't rely on fancy names and hidden state. But to answer your actual question: Yes, there is another way to do it. In all probability, gp_function is actually defined at module scope (I'm guessing here but it seems likely based on your description). Simply assign to the module's namespace before calling it - it'll "see" that as a global. ChrisA -- http://mail.python.org/mailman/listinfo/python-list