Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'package,': 0.03; 'argument': 0.05; '21,': 0.07; 'variables': 0.07; '[0,': 0.09; 'bindings': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; '(just': 0.16; 'arg': 0.16; 'bindings.': 0.16; 'closure.': 0.16; 'closures': 0.16; "function's": 0.16; 'pair,': 0.16; 'really?': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'roy': 0.16; 'wrote:': 0.18; '(not': 0.18; 'skip:f 30': 0.19; 'work,': 0.20; 'seems': 0.21; '>>>': 0.22; 'example': 0.22; 'separate': 0.22; 'header:User-Agent:1': 0.23; "shouldn't": 0.24; '(or': 0.24; 'environment': 0.24; 'header:X-Complaints- To:1': 0.27; 'van': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'originally': 0.30; '(which': 0.31; 'usually': 0.31; '13,': 0.31; '>>>>': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'writes:': 0.31; 'fri,': 0.33; 'sense': 0.34; 'maybe': 0.34; 'definition': 0.35; 'but': 0.35; 'described': 0.36; 'next': 0.36; 'subject:?': 0.36; 'should': 0.36; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'even': 0.60; 'free': 0.61; 'received:173': 0.61; 'information': 0.63; 'name': 0.63; 'more': 0.64; 'here': 0.66; 'close': 0.67; 'smith': 0.68; 'containing': 0.69; 'article': 0.77; 'received:fios.verizon.net': 0.84; 'snapshot': 0.84; 'from.': 0.93; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: closure = decorator? Date: Fri, 11 Oct 2013 16:55:33 -0400 References: <707e67a6-c398-4d0a-a058-76b8bf2829f0@googlegroups.com> <5257c3dd$0$29984$c3e8da3$5496439d@news.astraweb.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:24.0) Gecko/20100101 Thunderbird/24.0 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: 73 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1381525205 news.xs4all.nl 15969 [2001:888:2000:d::a6]:37187 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:56711 On 10/11/2013 8:08 AM, Franck Ditter wrote: > In article <5257c3dd$0$29984$c3e8da3$5496439d@news.astraweb.com>, > Steven D'Aprano wrote: > >> On Fri, 11 Oct 2013 10:14:29 +0300, Jussi Piitulainen wrote: >> >>> Roy Smith writes: >>>> In article , >>>> Piet van Oostrum wrote: >>>> >>>>> I usually say that a closure is a package, containing a function with >>>>> some additional data it needs. The data usually is in the form of >>>>> name bindings. >>>> >>>> That's pretty close to the way I think about it. The way it was >>>> originally described to me is, "A closure is a function bundled up with >>>> it's arguments". >>> >>> Really? It should be more like "a function bundled up with some other >>> function's arguments" and even more like "a function bundled up with >>> bindings for its free variables". >> >> Closures have nothing to do with *arguments*. A better definition of a >> closure is that it is a function together with a snapshot of the >> environment it was called from. >> >> def func(arg): >> y = arg + 1 >> def inner(): >> return y + 1000 >> return inner >> >> f = func(1) > > Maybe a better example of closure would be (just for the nonlocal) : > > def fib() : > (a,b) = (0,1) a,b = 0,1 is the same thing. a and b are separate local names and are in no sense a 'pair'. > def producer() : > nonlocal a,b # Python 3 > old = a > (a,b) = (b,a+b) > return old > return producer > >>>> f = fib() >>>> [f() for i in range(10)] > [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] > >> At this point, f is a closure. It needs to know the value of y (not the >> argument to func) in order to work, and the implementation is to store >> that information inside f.func_closure (or f.__closure__ in Python 3). >> The part of the calling environment which is saved is y > > Shouldn't it be the (a,b) pair here ? But : > >>>> f.__closure__[0].cell_contents # access to what ? > 55 > > Shouldn't cell_contents keep the current (a,b) pair, a part of the snapshot of > the creation environment (private variables of the closure) ? > Instead it seems to returns only a (which is the next production)... Look as f.__closure__[1] (.cell_contents) for b. -- Terry Jan Reedy