Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #56711

Re: closure = decorator?

From Terry Reedy <tjreedy@udel.edu>
Subject Re: closure = decorator?
Date 2013-10-11 16:55 -0400
References (3 earlier) <m2a9ihxf3a.fsf@cochabamba.vanoostrum.org> <roy-B93AED.20042310102013@news.panix.com> <qot38o88eju.fsf@ruuvi.it.helsinki.fi> <5257c3dd$0$29984$c3e8da3$5496439d@news.astraweb.com> <nobody-EDEEA0.14085711102013@news.free.fr>
Newsgroups comp.lang.python
Message-ID <mailman.1019.1381525205.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 10/11/2013 8:08 AM, Franck Ditter wrote:
> In article <5257c3dd$0$29984$c3e8da3$5496439d@news.astraweb.com>,
>   Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:
>
>> On Fri, 11 Oct 2013 10:14:29 +0300, Jussi Piitulainen wrote:
>>
>>> Roy Smith writes:
>>>> In article <m2a9ihxf3a.fsf@cochabamba.vanoostrum.org>,
>>>>   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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

closure = decorator? Tim <jtim.arnold@gmail.com> - 2013-10-10 06:51 -0700
  Re: closure = decorator? Chris Angelico <rosuav@gmail.com> - 2013-10-11 01:03 +1100
    Re: closure = decorator? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-10-10 17:15 +0300
      Re: closure = decorator? Piet van Oostrum <piet@vanoostrum.org> - 2013-10-10 12:31 -0400
        Re: closure = decorator? Roy Smith <roy@panix.com> - 2013-10-10 20:04 -0400
          Re: closure = decorator? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-10-11 10:14 +0300
            Re: closure = decorator? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-11 09:24 +0000
              Re: closure = decorator? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-10-11 15:01 +0300
                Re: closure = decorator? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-11 16:44 +0000
                Re: closure = decorator? Terry Reedy <tjreedy@udel.edu> - 2013-10-11 16:51 -0400
              Re: closure = decorator? Franck Ditter <nobody@nowhere.org> - 2013-10-11 14:08 +0200
                Re: closure = decorator? Terry Reedy <tjreedy@udel.edu> - 2013-10-11 16:55 -0400
  Re: closure = decorator? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-10-10 17:08 +0300
  Re: closure = decorator? Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-12 13:54 -0700
    Re: closure = decorator? Tim <jtim.arnold@gmail.com> - 2013-10-14 09:39 -0700

csiph-web