Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!newsfeed.tele2net.at!news.panservice.it!feed.xsnews.nl!border02.ams.xsnews.nl!feeder04.ams.xsnews.nl!abp001.ams.xsnews.nl!frontend-F10-17.ams.news.kpn.nl From: Cecil Westerhof Newsgroups: comp.lang.python Subject: Re: Wrote a memoize function: I do not mind feedback Organization: Decebal Computing References: <87h9s1zoap.fsf@Equus.decebal.nl> X-Face: "(y8cC@tg_12{">GF'UXTW]FHI2wMiZNrnf'1EFQ&O#$m:f#O7+7}kR,v+Pti8=Vi/Z"g^?b"E X-Homepage: http://www.decebal.nl/ Date: Wed, 29 Apr 2015 08:06:37 +0200 Message-ID: <87y4lbxyc2.fsf@Equus.decebal.nl> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:SBjr5tvMMRTac4b+Flp0uxm+t/Q= MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Lines: 51 NNTP-Posting-Host: 81.207.62.244 X-Trace: 1430288071 news.kpn.nl 1232 81.207.62.244@kpn/81.207.62.244:32993 Xref: csiph.com comp.lang.python:89518 Op Monday 27 Apr 2015 22:35 CEST schreef Albert-Jan Roskam: > ----- Original Message ----- >> From: Peter Otten <__peter__@web.de> >> To: python-list@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 That is in a way the same as what I do, except I do not use an exception. Iunderstand it is not as expensive as it was anymore, but I do not like to use an exception (when not necessary). -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof