Wrote a memoize function: I do not mind feedback
Albert-Jan Roskam
fomcl at yahoo.com
Mon Apr 27 16:35:18 EDT 2015
----- Original Message -----
> From: Peter Otten <__peter__ at web.de>
> To: python-list at python.org
> Cc:
> Sent: Monday, April 27, 2015 4:28 PM
> Subject: Re: Wrote a memoize function: I do not mind feedback
>
> Cecil Westerhof wrote:
>
>> I started again with Python. In Clojure you have memoize. I thought it
>> nice to have this in Python also. So I wrote it. With a Fibonacci
>> function to show the usefulness.
>> You can find it here:
>> https://github.com/CecilWesterhof/PythonLibrary
>>
>> I love to hear what you think of it.
>>
>> And ideas for other functionalities are welcome also. Übung macht den
>> Meister.
>
> See also:
>
> https://docs.python.org/dev/library/functools.html#functools.lru_cache
>
> A bit hard to find unless you already know it's there.
If you Google for memoization decorator, you will easily find many more implementations. Not 100 percent sure, but I think the LRU cache decorator was not exactly light weight. I also like this one from Martelli's Python Cookbook, where he uses a mutable default argument {a dictionary} to implement memoization. I believe I went like this:
def some_func(arg, _memoize={}):
try:
return _memoize[arg]
except KeyError:
result = some_expensive_operation(arg)
_memoize[arg] = result
return result
More information about the Python-list
mailing list