Function decorator that caches function results

Lasse Vågsæther Karlsen lasse at vkarlsen.no
Sat Oct 8 20:51:08 CEST 2005


George Sakkis wrote:
<snip>
> Cool, you re-invented the memoization pattern:
> http://en.wikipedia.org/wiki/Memoization
> http://aspn.activestate.com/ASPN/search?query=memoize&x=0&y=0&section=PYTHONCKBK&type=Subsection
> 
> Yes, it's kinda discouraging that most interesting ideas have already been conceived, implemented
> and used by others...<wink>
<snip>

I know, I've been scouring over the ASPN recipes and digging through 
code like there's no tomorrow.

But, I don't view it as pointless even though there are existing 
implementations and solutions out there.

First of all, I don't like to stuff a lot of what is obviously library 
type of code into a project, unless I can reference a library that got 
that function or class or whatnot. Creates a rather big maintenance 
nightmare :)

So, I would have to stuff that into a library, which is what I did with 
my "own" function (thank you for helping btw). The recipe on ASPN is 
probably better than what I got, but... I understand how my solution 
came about and what makes it tick.

Secondly, I've been "programming" Python for, what, about 11 days now or 
so, so I want to re-implement as much as possibly right now, even to the 
point where I create a worse solution than an existing one, as long as 
it works for me, just to be able to learn the nuances of Python, because 
Python is ... different than what I'm used to.

For instance, in C# and .NET you got attributes, but they don't actually 
do anything on their own, in other words you can't tag a method and have 
the operation of that method deviate from a similar method without the 
attribute, unless you pick one of the attributes the compiler knows 
about, so it's just meta-data that sits silent until some other method 
goes around to look for it.

In Python I've now learned that a function is just an object like 
everything else and I can wrap a new object around it to modify its 
behaviour, and I can use the decorator pattern to do it.

I'm probably going to be back here in a few days or even hours with 
another "task" where you can probably cough up dozens of existing source 
code solutions that I could use.

For instance, there's this thing I've heard of called the "wheel".....

:)

-- 
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:lasse at vkarlsen.no
PGP KeyID: 0x2A42A1C2



More information about the Python-list mailing list