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


Groups > comp.lang.python > #56621

Re: closure = decorator?

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: closure = decorator?
Date 2013-10-10 20:04 -0400
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-B93AED.20042310102013@news.panix.com> (permalink)
References <707e67a6-c398-4d0a-a058-76b8bf2829f0@googlegroups.com> <mailman.943.1381413788.18130.python-list@python.org> <qot8uy1rz4l.fsf@ruuvi.it.helsinki.fi> <m2a9ihxf3a.fsf@cochabamba.vanoostrum.org>

Show all headers | View raw


In article <m2a9ihxf3a.fsf@cochabamba.vanoostrum.org>,
 Piet van Oostrum <piet@vanoostrum.org> 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".

To make a real-life analogy, let's say you're modeling a parking garage.  
I want to be able to walk up to the attendant and say, "Please bring my 
car around front at 5 O'Clock.  It's that one" (pointing to the slightly 
dented green Ford in spot 37).  So, you've got a class:

class DeliveryRequest:
   def __init__(self, spot, time):
      self.spot = spot
      self.time = time

Now, over the course of the day, the garage attendants shuffle cars 
around to make room and retrieve vehicles that packed in the back.  
Comes 5 O'Clock, what vehicle do you want the attendant to deliver to 
the front?  The one that was in spot 37 at the time you made the 
request, or the one that's in spot 37 at 5 O'Clock?

Unless you want somebody else's car (perhaps you'd like something better 
than a slightly dented Ford), you want the attendant to capture the 
current state of spot 37 and remember that until 5 O'Clock when it's 
time to go get the car, no matter where it happens to be right now.

That's a closure.

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