[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

exec and locals

On Wed, 26 Feb 2014 14:00:59 +0000, Alister wrote:

> On Wed, 26 Feb 2014 13:15:25 +0000, 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 have no idea but as exec is generally considered to be a bad idea are
> you absolutely sure this is the correct way to achieve your end goal?
> perhaps if you detailed your requirement someone may be able to suggest
> a safer solution.

Thanks for your concern, but what I'm doing is perfectly safe. The string 
being exec'ed is a string literal known at compile-time and written by me 
(see my previous email for details) and the only reason I'm running it 
with exec at runtime rather than treating it as normal source code is 
that it relies on a feature that may not be available (with statement).

The joys of writing code that has to run under multiple incompatible 
versions of Python.