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


Groups > comp.lang.python > #56572

Re: closure = decorator?

References <707e67a6-c398-4d0a-a058-76b8bf2829f0@googlegroups.com>
Date 2013-10-11 01:03 +1100
Subject Re: closure = decorator?
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.943.1381413788.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Oct 11, 2013 at 12:51 AM, Tim <jtim.arnold@gmail.com> wrote:
> I've read a couple of articles about this, but still not sure.
> When someone talks about a closure in another language (I'm learning Lua on the side), is that the same concept as a decorator in Python?

No, they're quite different. A decorator (let's look at function
decorators; classes can have them too, and they work pretty much the
same way) is syntactic sugar for this:

def func(args):
    blah blah blah
func = decorator(func)

You can do all sorts of things with that. Even stupid things:

>>> @print
def foo():
pass

<function foo at 0x00F5D8A0>
>>> foo is None
True

Using print as a decorator does work, though hardly usefully :)

A closure, on the other hand, is a function that has some extra context:

def outer(x):
    x += 1
    def inner():
        return x
    return inner

The function inner() "knows" its context. When it's called, it'll use
the same value for x that would have been used in the outer function,
even though it's a separate function:

>>> foo = outer(5)
>>> foo()
6

The terminology is that inner() "closes over" x, if I have that
correct (I've not been all that big in functional programming and
lambda calculus). Someone will correct me if I'm not.

It's very common for a decorator to use closures, but the two are
completely different. They're no more connected than, say, for loops
and lists. They just happen to work well together.

Closures in other languages will, as far as I know, be the same thing
as closures in Python. (And their presence and functionality in
JavaScript leaves me wondering why on earth the 'this' reference can't
be considered "closed over" in the same way. But that's hardly the
worst of the language's warts.)

ChrisA

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