Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4a.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; '"this': 0.03; 'value,': 0.04; 'assignment': 0.07; 'compiler': 0.07; '"if': 0.09; 'function,': 0.09; 'prevents': 0.09; 'sake': 0.09; 'variable,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'language,': 0.12; 'creates': 0.14; '*only*': 0.16; 'assignment.': 0.16; 'closely.': 0.16; 'comments:': 0.16; 'declaration': 0.16; 'declared': 0.16; 'executed.': 0.16; 'false:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'it;': 0.16; 'variable.': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'examples': 0.20; 'code,': 0.22; 'cc:addr:python.org': 0.22; 'creating': 0.23; '(or': 0.24; 'cc:2**0': 0.24; 'header:In-Reply- To:1': 0.27; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'cases': 0.33; 'created': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'changing': 0.37; 'two': 0.37; 'being': 0.38; 'pm,': 0.38; 'rather': 0.38; 'even': 0.60; 'simple': 0.61; 'further': 0.61; 'telling': 0.64; 'more': 0.64; 'within': 0.65; 'here': 0.66; 'local,': 0.84; 'nonsense.': 0.84; 'to:none': 0.92 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=XG966iFCAUugfp3DPdWer3nw8yJjmtbNT80QPkfB9Rc=; b=svCEDwA4L95Zpb712MmFFVvJ5AbplZOPHIgXGHFYoqNleF1oe2b4CFTc2d2bjU6J47 6n9qc9QL0djcnFgLYBmyNuP2RClPu4Nzz1Lp/UkJTE0th2tBVwrl6xXSQgs+KK1Z13Gi r7E8uQUhnKs1JiKPYORGmL9PeDl24hUwsoRgZqfJJJU7WfJPHD8JfFizsx2cWaFzy4Gf jwBFeCNypKLolU460+ybRxZeGIZ0ei7nicU/huvTfTAw5djMZ4xGY3zkeEqHe3cNfmK6 t6JHM/Qd/U1afBNGbKHGgSznpvnLY8h6ciDxazIxmnWWQ3DYgbggaQGmbnNoq8slhLqv P1mg== MIME-Version: 1.0 X-Received: by 10.52.175.69 with SMTP id by5mr13780115vdc.16.1399785743966; Sat, 10 May 2014 22:22:23 -0700 (PDT) In-Reply-To: <536f069b$0$29980$c3e8da3$5496439d@news.astraweb.com> 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> <536eebc1$0$29980$c3e8da3$5496439d@news.astraweb.com> <536f069b$0$29980$c3e8da3$5496439d@news.astraweb.com> Date: Sun, 11 May 2014 15:22:23 +1000 Subject: Re: Values and objects From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1399785752 news.xs4all.nl 2879 [2001:888:2000:d::a6]:40079 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71289 On Sun, May 11, 2014 at 3:11 PM, Steven D'Aprano wrote: > Nonsense. Look at the original examples again, more closely. Here they > are again, this time with comments: > > def test(): > if False: spam = None # Dead code, never executed. > d = locals() > d['spam'] = 23 # Not a normal assignment. > return spam > > def test(): > locals()['spam'] = 42 # Not a normal assignment. > return spam > spam = None # Dead code. > > > The *only* purpose of the dead code in the two test() functions is to > force the compiler to use LOAD_FAST (or equivalent) rather than > LOAD_GLOBAL. In a C-like language, locals are created by a declaration, and assigned a value separately. In Python, locals are created by the presence of assignment within the function, which in simple cases coincides with giving the value to it; but guarding the assignment with "if False" prevents the giving of the value, while still being an assignment for the sake of creating a local variable. Same if the assignment happens further down, or even in the same statement ("spam = spam"). It's still an assignment, so it has the declarative effect of telling the compiler "this is now local, unless declared global/nonlocal". It's that declaration that creates the variable, not changing locals(). ChrisA