Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.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; 'context': 0.07; 'debugging': 0.07; 'nicely': 0.07; 'referring': 0.07; 'variables': 0.07; 'considered.': 0.09; 'deemed': 0.09; 'function,': 0.09; 'here?': 0.09; 'listing,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'variable,': 0.09; 'def': 0.12; 'jan': 0.12; 'language,': 0.12; 'times,': 0.14; 'closures': 0.16; 'dependent.': 0.16; 'devs': 0.16; 'dict': 0.16; 'local.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'statement.': 0.16; 'subject:variable': 0.16; 'survive': 0.16; 'usage,': 0.16; 'variables,': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'small,': 0.19; 'not,': 0.20; 'separate': 0.22; 'header:User-Agent:1': 0.23; 'body,': 0.24; 'source': 0.25; 'options': 0.25; 'least': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'leave': 0.29; 'am,': 0.29; "skip:' 10": 0.31; 'url:wiki': 0.31; 'apparently': 0.31; 'convenience': 0.31; 'names.': 0.31; 'obscure': 0.31; 'url:wikipedia': 0.31; 'class': 0.32; 'quite': 0.32; 'message.': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'accessible': 0.36; 'functions.': 0.36; 'keyword': 0.36; 'url:org': 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'rather': 0.38; 'little': 0.38; 'bad': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'dave': 0.60; 'free': 0.61; 'free,': 0.61; 'received:173': 0.61; 'first': 0.61; 'times': 0.62; 'term': 0.63; 'such': 0.63; 'within': 0.65; 'debate': 0.68; 'yourself': 0.78; 'local,': 0.84; 'received:fios.verizon.net': 0.84; 'angel': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Local variable in a closure Date: Sun, 18 Aug 2013 16:42:52 -0400 References: <107941d9-a981-4dd6-8460-336afc16f025@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 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: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1376858584 news.xs4all.nl 16003 [2001:888:2000:d::a6]:52378 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52662 On 8/18/2013 6:44 AM, Dave Angel wrote: > w.w.milner@googlemail.com wrote: > >> Is f local or not? >> http://pastebin.com/AKDJrbDs > > Please have a little respect, and include the source in your message. > You managed quite nicely to keep it small, but you put it in an obscure > place that some people won't be able to reach, and that might not > survive for the archives. > > def multiplier(f): > def times(n): > # is f local? > nonlocal f > f=f+1 > # if not, why is it here? > print("Locals: ",locals()) Because nonlocal names are not in the global dict and the devs wanted globals() + locals() to report all accessible names, rather than add nonlocals() or leave them invisible. > return n*f > return times > > times2 = multiplier(2) > print(times2(4)) # 3X4=12 > print(times2(4)) # 4X4=16 > > Inside function times, the variable 'f' is a free variable, not a local. > You can prove that to yourself by adding a dis.dis(times) just before > the "return times" statement. Here's how it begins: > > 7 0 LOAD_DEREF 0 (f) > 3 LOAD_CONST 1 (1) > 6 BINARY_ADD > 7 STORE_DEREF 0 (f) > > In the dis.dis listing, the LOAD_DEREF and STORE_DEREF opcodes are > referring to free variables, the LOAD_FAST is referring to a local, and > the LOAD_GLOBAL is referring to a global. > > The locals() function is just over-simplifying. it's only a convenience > function, not what I would consider part of the language, I think this is a good way to look at it. > and it wasn't apparently deemed necessary to have a separate function > for debugging free varaibles. One should think of 'locals' as meaning 'non_globals', which was exactly true when there were no non-global, non-local names. When closures were first added, such names were only readable. There was a long debate over what term to use for the keyword that would allow rebinding the names in outer functions. 'Nonlocal' is, at best, the least bad of the options considered. In standard usage, the terms 'free' and 'bound' are context dependent. https://en.wikipedia.org/wiki/Free_variable Within a function or class body, all global variables are non-local and free, just like 'nonlocals'. -- Terry Jan Reedy