Function decorator that caches function results

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sun Oct 9 11:58:39 EDT 2005


On Sun, 09 Oct 2005 13:43:38 +0200, Fredrik Lundh wrote:

>> 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:
> 
>     http://www.python.org/peps/pep-0227.html

That's interesting to read, and does clarify a few things that weren't
straight in my head, but I notice that the author, Jeremy Hylton,
distinguishes between Python creating function definitions that are
closures from those that aren't:

"Each def or lambda expression that is executed will create a closure if
the body of the function or any contained function has free variables."

Presumably that means that if there are no free variables, no closure
is created.


>> 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
> internals:
> 
>     http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440515

That's blackest voodoo, and as the warning says, could cause Python to
sigfault if you get the opcodes wrong.

I think that this is close enough to "really private" to satisfy most
people. Maybe even Paul Rubin *wink*


-- 
Steven.




More information about the Python-list mailing list