what is a closure?

Nick Trout nickatvideosystemdotcodotuk
Fri Dec 15 05:09:02 EST 2000


What a great resource! Thanks Fred.


"Fredrik Lundh" <fredrik at effbot.org> wrote in message
news:S18_5.3929$Kd1.455131 at newsb.telia.net...
> Nick Trout wrote:
> > Sorry, this is probably not the place to ask this but you lot are pretty
> > friendly :-)
>
> So you don't want me to flame you for not knowing about
> the free on-line dictionary of computing (foldoc)? ;-)
>
>     http://www.foldoc.org/
>
> > I have seen the term "closure" used... what is it exactly? Is it just a
> > class member function?
>
>     http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?query=closure
>
>     a closure is a data structure that holds an expression
>     and an environment of variable bindings in which that
>     expression is to be evaluated. The variables may be
>     local or global
>
> In Python, you have to use explicity syntax to move variables
> from an outer scope (environment) to the inner scope.
>
> In the following example, the "a=a" default argument binding
> binds add's local name "a" to the value of the "a" argument
> from make_adder.
>
>     def make_adder(a):
>         def add(b, a=a):
>             return a + b
>         return add
>
>     >>> m = make_adder(10)
>     >>> m(20)
>     30
>
> In a language with full lexical scoping, you could leave it to the
> compiler to figure this out.
>
> But in today's Python, it's probably better to use a class instead.
>
> </F>
>
> <!-- (the eff-bot guide to) the standard python library:
> http://www.pythonware.com/people/fredrik/librarybook.htm
> -->
>
>





More information about the Python-list mailing list