Function decorator that caches function results

Fredrik Lundh fredrik at
Sun Oct 9 13:43:38 CEST 2005

Steven D'Aprano wrote:

> I notice that type(some_closure) and type(ordinary_function) both return
> the same result, <type 'function'>. I also notice that both the closure
> and ordinary functions have an attribute "func_closure". Is there a way to
> tell which is a closure and which is not? In my tests, the ordinary
> function has func_closure == None, but I don't know if that will always be
> the case of just for my tests.

closure != function.

(did you read the wikipedia page?  the closure is a combination of the function
code itself, the information needed to call it, and the context it was defined in.
in Python, the full closure includes stuff you can reach via assorted attributes
on the function object; most notably func_closure and func_globals)

> If I wanted to inspect the value of cache, where would I find it?

nowhere.  at least no officially; see "Rebinding names in enclosing
scopes" in the design document for a discussion:

> That assumes that cache can be seen in this way. If it can't, is this a
> way to create "really private" variables in Python?

no, because Python doesn't prevent you from digging into the


More information about the Python-list mailing list