exec and locals
Steven D'Aprano
steve at pearwood.info
Wed Feb 26 23:47:33 EST 2014
On Wed, 26 Feb 2014 23:20:10 -0500, Dave Angel wrote:
> Before I would use exec, I'd look hard at either generating a
> source file to import,
Yes, I went through the process of pulling out the code into a separate
module, but that just made more complexity and was pretty nasty. If the
function was stand-alone, it might have worked, but it needed access to
other code in the module, so there were circular dependencies.
> or using a preprocessor.
I don't think that it's easier/better to write a custom Python
preprocessor and run the entire module through it, just to avoid a three-
line call to exec.
Guys, I know that exec is kinda dangerous and newbies should be
discouraged from throwing every string they see at it, but this isn't my
second day Python programming, and it's not an accident that Python
supports the dynamic compilation and execution of source code at runtime.
It's a deliberate language feature. We're allowed to use it :-)
> And if this code was
> to be installed, make the version choice or the preprocess step happen
> at install time.
Completely inappropriate in my case. This is a module which can be called
from multiple versions of Python from a single installation.
--
Steven
More information about the Python-list
mailing list