Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!ecngs!feeder2.ecngs.de!novso.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:not': 0.03; 'from:addr:yahoo.co.uk': 0.04; 'cest': 0.09; 'exception.': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'url:blog': 0.10; 'def': 0.12; 'language.': 0.14; '(when': 0.16; 'cheap,': 0.16; 'dict': 0.16; 'necessary).': 0.16; 'raised.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'suppose.': 0.16; 'exception': 0.16; 'do,': 0.16; 'language': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'trying': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'url:02': 0.24; 'wednesday': 0.24; 'gets': 0.27; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'raise': 0.29; "doesn't": 0.30; 'code': 0.31; '>>>>': 0.31; 'exceptions': 0.31; 'keyerror:': 0.31; 'url:06': 0.31; 'probably': 0.32; 'skip:_ 10': 0.34; 'except': 0.35; 'monday': 0.35; 'but': 0.35; 'useful': 0.36; 'performance': 0.37; 'to:addr:python-list': 0.38; 'expensive': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'ian': 0.60; "you're": 0.61; 'our': 0.64; 'linked': 0.65; 'charset:windows-1252': 0.65; '2015': 0.84; 'compare:': 0.84; 'received:2': 0.84; 'url:2013': 0.84; 'anymore,': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Wrote a memoize function: I do not mind feedback Date: Wed, 29 Apr 2015 09:58:44 +0100 References: <87h9s1zoap.fsf@Equus.decebal.nl> <87y4lbxyc2.fsf@Equus.decebal.nl> <87d22nxsvu.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-2-98-195-171.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 In-Reply-To: <87d22nxsvu.fsf@Equus.decebal.nl> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430297954 news.xs4all.nl 2904 [2001:888:2000:d::a6]:51211 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89531 On 29/04/2015 09:04, Cecil Westerhof wrote: > Op Wednesday 29 Apr 2015 09:02 CEST schreef Ian Kelly: > >> On Wed, Apr 29, 2015 at 12:06 AM, Cecil Westerhof wrote: >>> Op Monday 27 Apr 2015 22:35 CEST schreef Albert-Jan Roskam: >>>> 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). >> >> It's useful to keep in mind which case it is that you're trying to >> optimize. The expensive case for exceptions is when one actually >> gets raised. A try that doesn't raise an exception is pretty cheap, >> probably cheaper than looking up the key in the dict twice as the >> code you linked does. Compare: > > It is not only performance wise, I find the code without the try also > better looking. But that is very personally I suppose. > You might find this interesting http://www.jeffknupp.com/blog/2013/02/06/write-cleaner-python-use-exceptions/ -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence