closure = decorator?

Jussi Piitulainen jpiitula at ling.helsinki.fi
Thu Oct 10 10:08:32 EDT 2013


Tim writes:

> 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?
> 
> It sure looks like it.

I don't see how. Wikipedia's opening paragraph on "closure" seems good
to me - closures are a way to implement lexical scoping when functions
that have free variables are passed as arguments and returned as
values:

<http://en.wikipedia.org/wiki/Closure_(computer_science)>
# In programming languages, a closure (also lexical closure or
# function closure) is a function or reference to a function together
# with a referencing environment—a table storing a reference to each
# of the non-local variables (also called free variables or upvalues)
# of that function.[1] A closure—unlike a plain function
# pointer—allows a function to access those non-local variables even
# when invoked outside its immediate lexical scope.

There's an example in Python on that page.



More information about the Python-list mailing list