Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3a.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'dynamically': 0.07; 'explicit': 0.07; 'exec': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'attempted.': 0.16; 'bytecode': 0.16; 'exec_stmt': 0.16; 'globals(),': 0.16; 'instead:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'work,': 0.20; '>>>': 0.22; 'appears': 0.22; 'example': 0.22; 'header:User-Agent:1': 0.23; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; "i'm": 0.30; 'code': 0.31; '(my': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'trivial': 0.31; 'work:': 0.31; 'url:python': 0.33; 'created': 0.35; 'but': 0.35; 'described': 0.36; 'url:org': 0.36; 'should': 0.36; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'according': 0.40; 'url:3': 0.61; 'act': 0.63; 'guarantee': 0.63; 'real': 0.63; 'more': 0.64; 'here': 0.66; 'yes': 0.68; 'below:': 0.68; 'default': 0.69; 'url:4': 0.69; 'returns.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: exec and locals Date: Wed, 26 Feb 2014 14:46:39 +0100 Organization: None References: <530de8ed$0$29985$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bdbb62.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 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: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393422407 news.xs4all.nl 2833 [2001:888:2000:d::a6]:41850 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67094 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. > > Here is a trivial example: > > > py> def spam(): > ... exec( """x = 23""" ) > ... return x > ... > py> spam() > 23 > > > (My real example is more complex than this.) > > According to the documentation of exec, I don't think this should > actually work, and yet it appears to. The documentation says: > > The default locals act as described for function locals() > below: modifications to the default locals dictionary should > not be attempted. Pass an explicit locals dictionary if you > need to see effects of the code on locals after function > exec() returns. > > http://docs.python.org/3.4/library/functions.html#exec > > > I *think* this means that if I want to guarantee that a local variable x > is created by exec, I need to do this instead: > > py> def eggs(): > ... mylocals = {} > ... exec( """x = 23""", globals(), mylocals) > ... x = mylocals['x'] > ... return x > ... > py> eggs() > 23 > > The fact that it works in spam() above is perhaps an accident of > implementation? Yes no maybe? eggs() should work in Python 2 and 3, spam() should work in Python 2, but not in Python 3. Fun fact: Python 2 tweaks the bytecode (LOAD_NAME instead of LOAD_GLOBAL) to make spam() work: >>> def spam(): ... return x ... >>> dis.dis(spam) 2 0 LOAD_GLOBAL 0 (x) 3 RETURN_VALUE >>> def spam(): ... exec "" ... return x ... >>> dis.dis(spam) 2 0 LOAD_CONST 1 ('') 3 LOAD_CONST 0 (None) 6 DUP_TOP 7 EXEC_STMT 3 8 LOAD_NAME 0 (x) 11 RETURN_VALUE