Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'interpreter': 0.05; 'assignment': 0.07; 'explicit': 0.07; 'interpreter.': 0.07; 'modify': 0.07; 'variables': 0.07; 'doing?': 0.09; 'exec': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'referenced': 0.09; 'runs': 0.10; 'def': 0.12; '2.7': 0.14; 'changes': 0.15; 'attempted.': 0.16; 'false:': 0.16; 'locals():': 0.16; 'received:80.91.229.3': 0.16; 'received:dyn.optonline.net': 0.16; 'received:optonline.net': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.18; 'variable': 0.18; '>>>': 0.22; 'header:User- Agent:1': 0.23; 'example.': 0.24; 'pass': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'code': 0.31; '"",': 0.31; "d'aprano": 0.31; 'doc': 0.31; 'steven': 0.31; 'file': 0.32; 'running': 0.33; '(most': 0.33; 'test': 0.35; 'described': 0.36; 'should': 0.36; 'being': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'explain': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'free': 0.61; 'affect': 0.61; 'act': 0.63; 'note:': 0.66; 'statement,': 0.68; 'below:': 0.68; 'default': 0.69; 'dict.': 0.84; 'returns.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ned Batchelder Subject: Re: Values and objects Date: Sun, 11 May 2014 14:40:24 -0400 References: <235C4BFA-9770-481A-9FCF-21C3F036769C@gmail.com> <87ppjpwafk.fsf@elektro.pacujo.net> <536ad8f2$0$29965$c3e8da3$5496439d@news.astraweb.com> <87zjiqbmy5.fsf@elektro.pacujo.net> <536d7a7d$0$29980$c3e8da3$5496439d@news.astraweb.com> <9cc0ebf9-dbed-4d3d-91fc-2abb9b0103d0@googlegroups.com> <536dc3f7$0$29980$c3e8da3$5496439d@news.astraweb.com> <536decca$0$29980$c3e8da3$5496439d@news.astraweb.com> <536E799D.6080602@stoneleaf.us> <536eea59$0$29980$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: ool-4356373c.dyn.optonline.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 In-Reply-To: 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: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1399833638 news.xs4all.nl 2927 [2001:888:2000:d::a6]:40451 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71334 On 5/11/14 9:46 AM, Rotwang wrote: > On 11/05/2014 04:11, Steven D'Aprano wrote: >> [...] >> >> And try running >> this function in both 2.7 and 3.3 and see if you can explain the >> difference: >> >> def test(): >> if False: x = None >> exec("x = 1") >> return x > > I must confess to being baffled by what happens in 3.3 with this > example. Neither locals() nor globals() has x = 1 immediately before the > return statement, so what is exec("x = 1") actually doing? > The same happens if you try to modify locals(): >>> def test(): ... if 0: x = 1 ... locals()['x'] = 13 ... return x ... >>> test() Traceback (most recent call last): File "", line 1, in File "", line 4, in test UnboundLocalError: local variable 'x' referenced before assignment The doc for exec says: Note: 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. The doc for locals says: Note: The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter. This is a tradeoff of practicality for purity: the interpreter runs faster if you don't make locals() a modifiable dict. -- Ned Batchelder, http://nedbatchelder.com