Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'algorithm': 0.03; 'arguments': 0.07; 'decorator': 0.07; 'ugly': 0.07; 'caching,': 0.09; 'logic': 0.09; 'solution,': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'subject:not': 0.11; 'algorithm.': 0.16; 'caching': 0.16; 'personally,': 0.16; 'prefer.': 0.16; 'then?': 0.16; 'mathematical': 0.17; 'saying': 0.18; 'sort': 0.21; 'cc:2**0': 0.23; 'example': 0.23; 'cc:no real name:2**0': 0.24; 'second': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'chris': 0.28; 'trouble': 0.28; 'though.': 0.29; 'url:mailman': 0.29; 'definition': 0.29; "i'm": 0.29; 'maybe': 0.29; 'url:python': 0.32; 'help,': 0.32; 'could': 0.32; 'url:listinfo': 0.32; 'version': 0.34; 'received:google.com': 0.34; 'clear': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'url:org': 0.36; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'talk': 0.38; 'header:Received:5': 0.40; 'url:mail': 0.40; 'think': 0.40; 'your': 0.60; 'easy': 0.60; 'close': 0.63; 'skip:n 10': 0.63; 'more': 0.63; 'as:': 0.75; 'algorithm,': 0.84; 'iterative': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=6+S9HmfNtfWFUgMYadi+we5UoLElnY7YDGexygqSR/k=; b=y+K64uXEnwqYvzKiCBcAQOBcndThHDM2HhXNEnmy6T9lf9TetrbBbQY9mC48UfULGQ QCwTtS6O1wl7ftvyKVEDmeiQkJePa7Z7gQAIO8LuGC64z19sA45QuZ7Y1TpvjHQPTG2O jjoUNtJieow90LR9BKX5qKkoXFc/ye6cZzCwGtaPjECwJj6ZMFE9GVausr4BFvonnv+U H67YV55bLF79XhQyLLFaW0bxivIGj6xVG+6L/YzRGYoapEdXNcIts5Ow8cpwRqO01CLr x8D7ZCjJAI27P/6RIQAyNnOtyOqLqzVCgqsOAR3wrqUsMvwtME2G8n8bcrFRuPBxQUDB QH4A== MIME-Version: 1.0 In-Reply-To: References: <1017333532.1009581.1347628946603.JavaMail.root@sequans.com> Date: Fri, 14 Sep 2012 17:15:35 +0100 Subject: Re: Decorators not worth the effort From: andrea crotti To: Chris Angelico Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347639338 news.xs4all.nl 6874 [2001:888:2000:d::a6]:49917 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29178 2012/9/14 Chris Angelico : > > Trouble is, you're starting with a pretty poor algorithm. It's easy to > improve on what's poor. Memoization can still help, but I would start > with a better algorithm, such as: > > def fib(n): > if n<=1: return 1 > a,b=1,1 > for i in range(1,n,2): > a+=b > b+=a > return b if n%2 else a > > def fib(n,cache=[1,1]): > if n<=1: return 1 > while len(cache)<=n: > cache.append(cache[-1] + cache[-2]) > return cache[n] > > Personally, I don't mind (ab)using default arguments for caching, but > you could do the same sort of thing with a decorator if you prefer. I > think the non-decorated non-recursive version is clear and efficient > though. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list The poor algorithm is much more close to the mathematical definition than the smarter iterative one.. And in your second version you include some ugly caching logic inside it, so why not using a decorator then? I'm not saying that with the memoization is the "good" solution, just that I think it's a very nice example of how to use a decorator, and maybe a good example to start with a talk on decorators..