Path: csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!usenetcore.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!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; 'subject:Python': 0.05; 'defaults': 0.05; 'used.': 0.05; 'assignment': 0.07; 'dict': 0.09; 'given,': 0.09; 'globals': 0.09; 'def': 0.13; 'ah,': 0.16; 'defaulting': 0.16; 'empty),': 0.16; 'happily': 0.16; 'namespace.': 0.16; 'wrote:': 0.16; '(in': 0.18; '2015': 0.20; 'exec': 0.22; 'sep': 0.22; 'am,': 0.23; 'sat,': 0.23; 'header:In- Reply-To:1': 0.24; 'chris': 0.26; 'fri,': 0.27; 'updating': 0.27; 'message-id:@mail.gmail.com': 0.27; '[2]': 0.27; 'function': 0.28; 'starts': 0.29; '[1]': 0.32; 'returned': 0.32; 'source': 0.33; 'received:google.com': 0.35; 'but': 0.36; 'created': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; '12,': 0.37; 'seem': 0.37; 'does': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'called': 0.40; 'more': 0.63; 'within': 0.64; 'locals': 0.84; 'thing...': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=iFBvTDmAbYFWiKGCqzUN33ym9s83CFfTgnmK7okyOOE=; b=No0RLaqIpYzyLkxZp3VDmco3JH4Dzl7aAhYqmZdqnraNoYWqS1KxLC/AXRewimTLVr FC9mWTpYaQ9C9fqfCkX+B14bgkPgBxlBws75t0T5afunWtfRGVP4sfTOgss94/uhtN4e boxQB7eiW3oHvOMOpv7gYXYKJ1cf4zbNcG9mPn3H370Bt+Sr8lRgyLzDL6sBCswibcTa b7bYWonxdMidhcN8Atjz7x8re2eBEQBJzOTFYKdsHeDAu7+IkLoEVmiXIeVytm5hjzro LwJojb0rVkrIGG6erRAqyYs1nn/W9hjvL8onRvWbZGJ23aBf451AZ8l6Ls4iC0tOX/8a +r6A== X-Received: by 10.129.103.67 with SMTP id b64mr26793832ywc.55.1441986626791; Fri, 11 Sep 2015 08:50:26 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <55f293da$0$1640$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Fri, 11 Sep 2015 09:49:47 -0600 Subject: Re: Python handles globals badly. To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1441986629 news.xs4all.nl 23764 [2001:888:2000:d::a6]:40428 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:96349 On Fri, Sep 11, 2015 at 9:44 AM, Chris Angelico wrote: > On Sat, Sep 12, 2015 at 1:27 AM, Ian Kelly wrote: >> The exec still happily runs; it's just using its own private locals namespace. >> >> Tangent: does the help for exec need to be updated? It currently reads: >> >> The globals and locals are dictionaries, defaulting to the current >> globals and locals. If only globals is given, locals defaults to it. >> >> Which would seem to indicate that if called from within a function >> with no globals or locals, the locals from the function would be used. > > And that's the thing... I think. It's using locals(), which starts out > as a copy of the function's locals (in this example, empty), but > without assignment affecting anything. Which is more than a little > weird: > >>>> def f(): > ... x = [1] > ... exec("print(x); x[0] = 2; print(x); x = [3]; print(x)") > ... print(x) > ... >>>> f() > [1] > [2] > [3] > [2] Ah, that makes sense. It's writing into the dict that is created and returned by locals(), but not actually updating the frame locals which are the source of truth.