Function decorator that caches function results

Fredrik Lundh fredrik at pythonware.com
Sun Oct 9 08:27:32 EDT 2005


Steven D'Aprano wrote:

> Yes I did. Did you read my post?

given that the wikipedia page says

    "a closure is an abstraction representing a function, plus the
    lexical environment (see static scoping) in which the function
    was created."

and you're still focussing on type(function), it sure looks as if you missed
certain parts of that explanation.

let's take it again, with emphasis on some important words:

    "a closure is an ABSTRACTION representing a function, PLUS the
    lexical ENVIRONMENT (see static scoping) in which the function
    was created."

> If you create a closure, using a memoization technique as per the original
> post, and then call type() on that closure, Python reports <type 'function'>.

that's because "closure" is an abstract concept.  there is no "closure" object
in Python, just as there is no "program" object.  function objects always con-
tain all the information they need about their context, and the sum of that is
what forms the closure.

> If you use dir() on that closure-that-Python-calls-a-function, it tells
> you that there is an attribute "func_closure". But ordinary functions that
> aren't closures also have that attribute.

"ordinary functions" also have closures: the global context is also part of
the function's environment, and therefore part of the closure (in Python,
it can be argued that all attributes of the function object are part of the
closure)

</F>






More information about the Python-list mailing list