exec and locals

Dave Angel davea at davea.name
Thu Feb 27 05:20:10 CET 2014


 Steven D'Aprano <steve at pearwood.info> Wrote in message:
> On Wed, 26 Feb 2014 14:46:39 +0100, Peter Otten wrote:
> 
>> Steven D'Aprano wrote:
>> 
>>> I have to dynamically generate some code inside a function using exec,
>>> but I'm not sure if it is working by accident or if I can rely on it.
>>> 
>> I eventually settled on some 
> code that works something like this:
> 
> 
> def factory():
>     blah blah blah
>     try:
>         exec("""def inner():
>                     

Before I would use exec,  I'd look hard at either generating a
 source file to import,  or using a preprocessor.  And if this
 code was to be installed,  make the version choice or the
 preprocess step happen at install time.

I once implemented a system that generated 20k lines of C++ header
 and sources. And the generated code was properly indented and
 fairly well commented. 



-- 
DaveA




More information about the Python-list mailing list