Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'dynamically': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'def': 0.12; 'wrote': 0.14; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sources.': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'properly': 0.19; 'feb': 0.22; '>>>': 0.22; 'install': 0.23; 'fairly': 0.24; 'header': 0.24; 'source': 0.25; 'this:': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; "i'm": 0.30; 'code': 0.31; 'lines': 0.31; '+0100,': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'file': 0.32; 'implemented': 0.33; "i'd": 0.34; 'something': 0.35; 'but': 0.35; 'version': 0.36; 'c++': 0.36; 'step': 0.37; 'to:addr:python-list': 0.38; 'generating': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'eventually': 0.60; 'happen': 0.63; 'otten': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re: exec and locals Date: Wed, 26 Feb 2014 23:20:10 -0500 (EST) Organization: news.gmane.org References: <530de8ed$0$29985$c3e8da3$5496439d@news.astraweb.com> <530e8608$0$11113$c3e8da3@news.astraweb.com> X-Gmane-NNTP-Posting-Host: dpc6744193198.direcpc.com X-Newsreader: PiaoHong Usenet NewsReaders 1.36 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393474578 news.xs4all.nl 2840 [2001:888:2000:d::a6]:42950 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67112 Steven D'Aprano 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