Modify the local scope inside a function

Sandra-24 sandravandale at yahoo.com
Sun Feb 26 23:25:14 EST 2006


Hey Crutcher, thanks for the code, that would work. I'm now debating
using that, or using function arguments to get the variables into the
namespace. This would require knowing the variables in the dict ahead
of time, but I suppose I can do that because it's part of the same
system that creates the dict. I'm just not very fond of having code
relating to one thing in more than one place, because it puts the onus
on the programmer to remember to change it in both places. Here I might
forgive it because it would make the generated code more readable.

It seems I created a fair amount of confusion over what I'm trying to
do. I use special psp like templates in my website. The template engine
was previously execing the generated template code. It uses special
environment variables that give it access to the functionality of the
web engine. These are what are in that scope dictionary of mine, and
why I exec the code in that scope.

However, I want to integrate a debugger with the web engine now, and
debugging execed generated code is a nightmare. So I save the generated
code as a function in a module that is generated by the template
engine. Unless I'm missing something about what you're saying, this
should now be faster as well, because afaik execed code has to be
compiled on the spot, wheras a module when you load it, is compiled (or
loaded from a .pyc file) at import time. So one import and repeated
function calls would be cheaper than repeated exec.

Thanks,
-Sandra




More information about the Python-list mailing list