Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!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; 'debugging': 0.07; 'nicely': 0.07; 'referring': 0.07; '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; 'language,': 0.12; 'times,': 0.14; 'local.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'statement.': 0.16; 'subject:variable': 0.16; 'survive': 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; 'source': 0.25; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'apparently': 0.31; 'convenience': 0.31; 'obscure': 0.31; 'quite': 0.32; 'message.': 0.35; 'but': 0.35; 'charset:us-ascii': 0.36; 'to:addr:python-list': 0.38; 'little': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'free': 0.61; 'times': 0.62; 'yourself': 0.78; 'local,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re: Local variable in a closure Date: Sun, 18 Aug 2013 10:44:06 +0000 (UTC) References: <107941d9-a981-4dd6-8460-336afc16f025@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 174.32.174.30 User-Agent: XPN/1.2.6 (Street Spirit ; Linux) 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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1376822665 news.xs4all.nl 15865 [2001:888:2000:d::a6]:34150 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52655 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()) 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, and it wasn't apparently deemed necessary to have a separate function for debugging free varaibles. -- DaveA