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!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'compiler': 0.05; 'none,': 0.05; 'none)': 0.07; 'already.': 0.09; 'builtins': 0.09; 'global,': 0.09; 'globals': 0.09; 'specifying': 0.09; 'variants': 0.09; 'python': 0.10; 'argument': 0.15; 'explicitly': 0.15; 'thu,': 0.15; '(rather': 0.16; '4:25': 0.16; 'closures': 0.16; "compiler's": 0.16; 'distinction': 0.16; 'eval': 0.16; 'namespace.': 0.16; 'run-time.': 0.16; 'wrote:': 0.16; 'looked': 0.16; '>>>': 0.20; '2015': 0.20; 'fairly': 0.22; 'exec': 0.22; 'function,': 0.22; 'sep': 0.22; 'am,': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; "doesn't": 0.26; 'fri,': 0.27; 'order.': 0.27; 'right.': 0.27; 'message- id:@mail.gmail.com': 0.27; 'declared': 0.29; 'rules': 0.31; 'knows': 0.32; 'compiled': 0.32; 'maybe': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'received:google.com': 0.35; 'identified': 0.35; 'star': 0.35; 'quite': 0.35; 'according': 0.36; 'but': 0.36; 'list,': 0.36; '(and': 0.36; 'assigned': 0.36; 'to:addr:python- list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'things': 0.38; 'mean': 0.38; 'to:addr:python.org': 0.40; 'still': 0.40; 'within': 0.64; 'between': 0.65; 'therefore': 0.67; 'huh?': 0.84; 'simple:': 0.84; 'specialised': 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=bj2WMCHHcVr9cpNUEQCfnr3tXSMbE23qDKM+cBiTD2E=; b=K2aQnohXe2580JFgHdAIGWDaWAsZIYnREwKDzu8cJve03yaKpGFFA+dhd7IIPGOwSG x9bYTn4jEZNuvTbsUFx7fuV9XQHHIWhL63ji9pxLptvadhCojFq4akJJQiudjH2aZgr4 arL61SntMQv9Xsq5l2lxW6QNOSvnckx9RTYpg3srIlKTR8PiL+LqPuHld/MITfxQ4c69 sn1EtDiBToUGhMV2wevOqjCb0dTmx4wltfJJ898Fpy27kvxAnOaFKvpJWC8MLPq0Hogq bA1TlQL0ULHiTtzYldzBH6/SC1MtsBeJBdc4jc0tlVBLln4peKG7r0hvUoXh2S05O024 Xh8A== X-Received: by 10.129.145.214 with SMTP id i205mr7553259ywg.64.1441983861112; Fri, 11 Sep 2015 08:04:21 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <55f293da$0$1640$c3e8da3$5496439d@news.astraweb.com> References: <55f293da$0$1640$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Fri, 11 Sep 2015 09:03:41 -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: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1441983869 news.xs4all.nl 23727 [2001:888:2000:d::a6]:55054 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:96345 On Fri, Sep 11, 2015 at 2:42 AM, Steven D'Aprano wrote: > On Fri, 11 Sep 2015 10:35 am, Ian Kelly wrote: > >> On Thu, Sep 10, 2015 at 4:25 PM, wrote: > [...] >>> So the compiler knows the distiction between global and local already. >> >> As we've said before, it doesn't. The compiler's current rules are >> fairly simple: >> >> 1) If it's in the function's argument list, it's an argument (and >> therefore local). >> 2) If it's explicitly declared global, then it's global. >> 3) If it's never assigned within the function, then it's global. > > Almost. If it's never assigned within the function, then it is looked up > according to the non-local scoping rules: > > - closures and enclosing functions (if any); > - globals; > - builtins; > > in that order. I excluded non-locals intentionally, but if you want to be pedantic about it, then that's still not quite right. Non-locals are indeed identified by the compiler and compiled with the LOAD_DEREF/STORE_DEREF opcodes (rather than the _GLOBAL and _FAST variants used by globals and locals, respectively). The compiler doesn't make any such distinction between globals and builtins however, as that can only be determined at run-time. > There's also a bunch of specialised and complicated rules for what happens > if you make a star import ("from module import *") inside a function, or > call eval or exec without specifying a namespace. Both of these things are > now illegal in Python 3. Huh? >>> exec("x = 42") >>> x 42 >>> exec("x = 43", None, None) >>> x 43 That's in Python 3.4.0. Maybe I don't understand what you mean by "without specifying a namespace".