Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'algorithm': 0.03; 'decorator': 0.07; 'ugly': 0.07; 'logic': 0.09; 'received:mail- vb0-f46.google.com': 0.09; 'sep': 0.09; 'stack,': 0.09; 'subject:not': 0.11; '(the': 0.15; 'sat,': 0.15; 'caching': 0.16; 'definition.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'mathematics.': 0.16; 'one)': 0.16; 'safely.': 0.16; 'sequence,': 0.16; 'then?': 0.16; 'version;': 0.16; 'wrote:': 0.17; 'mathematical': 0.17; 'received:209.85.212.46': 0.18; '15,': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'scale': 0.27; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; 'coded': 0.29; 'definition': 0.29; 'function': 0.30; 'to:addr:python-list': 0.33; 'version': 0.34; 'received:google.com': 0.34; 'received:209.85': 0.35; 'but': 0.36; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'close': 0.63; 'ever': 0.63; 'more': 0.63; 'learned': 0.65; 'andrea': 0.84; 'blowing': 0.84; 'inherent': 0.84; 'iterative': 0.84; 'fibonacci': 0.91 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 :content-type; bh=jweMbuptia5x8J7ahAJFU3yE0dNjdo/rpq/8ncCh6no=; b=YwRAPY1jolxrg98zS9Nvq8TBn1+7LYpoQrsLBghpbIBGuz0fK5YjV8L4gO3uOAlT7G lac/hnDjp/YMPQvxDt67oI9XLuOrNzSLNhp57io/2+zSTzpsufOxiBOMaf5+ho0701rO YNAP3nyTxXFgu//H4lDF+Ybj/CXT77qOqde+pY7u9Ejd+Z3aBGXddLy5eM3kfZ7lfPhL hoBhgKxo1Dzvn+0xYMeq8mX8Jq0bQeKEUQKkVOxt/Tdio22/OrujbqLESyWhup0NSUHH JMp/z48E6khZPgK88hPqUn/DCum6MxjqW0dV3XEF2ZMGSwDrtMX92FHeuxkEOnIX5ZD0 +wBw== MIME-Version: 1.0 In-Reply-To: References: <1017333532.1009581.1347628946603.JavaMail.root@sequans.com> Date: Sat, 15 Sep 2012 03:30:12 +1000 Subject: Re: Decorators not worth the effort From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 21 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347643815 news.xs4all.nl 6845 [2001:888:2000:d::a6]:49065 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29182 On Sat, Sep 15, 2012 at 2:15 AM, andrea crotti wrote: > 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 learned Fibonacci as a sequence, not as a recursive definition. So the algorithm I coded (the non-caching one) is pretty much how I learned it in mathematics. But yes, you're right that the caching is inherent to the second version; and yes, that's where a decorator can make it a LOT cleaner. As a demo of recursion and decorators, your original function pair is definitely the best. But if you want to be able to calculate fib(n) for any n without blowing your stack, my version will scale much more safely. But then again, who actually ever needs fibonacci numbers? ChrisA