Path: csiph.com!news.mixmin.net!feeds.phibee-telecom.net!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; 'cc:addr :python-list': 0.09; 'given,': 0.09; 'globals': 0.09; 'def': 0.13; 'cc:name:python': 0.16; 'defaulting': 0.16; 'empty),': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'happily': 0.16; 'namespace.': 0.16; 'received:mail- ig0-x22a.google.com': 0.16; 'wrote:': 0.16; '(in': 0.18; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'exec': 0.22; 'sep': 0.22; 'am,': 0.23; 'sat,': 0.23; 'header:In- Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; '[2]': 0.27; 'function': 0.28; 'starts': 0.29; "can't": 0.32; '[1]': 0.32; 'except': 0.34; 'received:google.com': 0.35; 'but': 0.36; 'subject:: ': 0.37; '12,': 0.37; 'seem': 0.37; 'version': 0.38; 'does': 0.39; 'still': 0.40; 'called': 0.40; 'back': 0.62; 'more': 0.63; 'within': 0.64; 'chrisa': 0.84; 'locals': 0.84; 'thing...': 0.84; 'to:none': 0.91; 'shadow': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=Xsz45SVd04jTI5tF50iAoW18Ag13s1XC8uUJay3QEGs=; b=YJxgTAxxf/XWTLIrJKuMUL9V0i+d5AhHDHtfWrebpEI5FfXPZLUyAlm4zLz8VHp9vy 09hgYNUqMFNWZVmLjC6wljCiHFXOL9vtczxyvshUM1l6m9HHd6FdXiJv1pdKj34rQKyZ haRHi48iiU9lhjAFjlDyAL3/5D4SMiJBk0zdCqQgXwrWJVeEqX1KTCYgZg2M1Orb49kA TeLw5H5yZoK0lMbj2yoaJURJge0ZmrR+qdeu1lLH03mpNIDtJ5dsIcL8x53AXflqtvP3 sD0IdAArwk8CxztrdyItb3dGnHlJMjfQioHU4e4UoaHn7/XFMD4tKlcAPe8LRX8BtPp2 9G2Q== MIME-Version: 1.0 X-Received: by 10.50.3.66 with SMTP id a2mr5169037iga.92.1441986257596; Fri, 11 Sep 2015 08:44:17 -0700 (PDT) In-Reply-To: References: <55f293da$0$1640$c3e8da3$5496439d@news.astraweb.com> Date: Sat, 12 Sep 2015 01:44:17 +1000 Subject: Re: Python handles globals badly. From: Chris Angelico Cc: 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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1441986260 news.xs4all.nl 23801 [2001:888:2000:d::a6]:37147 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:96348 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] It's kinda like how globals can shadow builtins, I think. Maybe. Except that you can't del the name inside exec to unshadow and go back to the outer version of it. ChrisA