Path: csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed.kamp.net!newsfeed.freenet.ag!news2.euro.net!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; 'python.': 0.02; 'arguments': 0.07; 'decorator': 0.07; 'elegant': 0.07; 'overflow': 0.07; 'caching,': 0.09; 'sep': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'subject:not': 0.11; 'sat,': 0.15; 'stack': 0.15; 'algorithm.': 0.16; 'iterators': 0.16; 'personally,': 0.16; 'prefer.': 0.16; 'wrote:': 0.17; 'sort': 0.21; 'cc:2**0': 0.23; 'example': 0.23; '15,': 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; 'header :User-Agent:1': 0.26; 'looks': 0.26; 'am,': 0.27; 'plain': 0.27; 'received:209.85.212': 0.28; 'chris': 0.28; 'trouble': 0.28; 'though.': 0.29; 'help,': 0.32; 'could': 0.32; 'version': 0.34; 'received:google.com': 0.34; 'clear': 0.35; 'from:addr:googlemail.com': 0.35; 'received:209.85': 0.35; 'add': 0.36; 'but': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'think': 0.40; 'easy': 0.60; 'first': 0.61; 'back': 0.62; 'skip:n 10': 0.63; 'more': 0.63; 'become': 0.65; 'as:': 0.75; 'algorithm,': 0.84; 'andrea': 0.84; '8bit%:70': 0.91; 'fibonacci': 0.91; 'received:209.85.212.56': 0.91 Newsgroups: comp.lang.python Date: Fri, 14 Sep 2012 09:37:37 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=123.192.32.215; posting-account=5JdMBQoAAABHnS4mjpqEzxnmWtgiiVNw References: <1017333532.1009581.1347628946603.JavaMail.root@sequans.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 123.192.32.215 MIME-Version: 1.0 Subject: Re: Decorators not worth the effort From: 88888 Dihedral To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: , Message-ID: Lines: 94 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347640666 news.xs4all.nl 6859 [2001:888:2000:d::a6]:38042 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29181 Chris Angelico=E6=96=BC 2012=E5=B9=B49=E6=9C=8814=E6=97=A5=E6=98=9F=E6=9C= =9F=E4=BA=94UTC+8=E4=B8=8B=E5=8D=8810=E6=99=8241=E5=88=8606=E7=A7=92=E5=AF= =AB=E9=81=93=EF=BC=9A > On Sat, Sep 15, 2012 at 12:12 AM, andrea crotti >=20 > wrote: >=20 > > def fib(n): >=20 > > if n <=3D 1: >=20 > > return 1 >=20 > > return fib(n-1) + fib(n-2) >=20 > > >=20 > > @memoize >=20 > > def fib_memoized(n): >=20 > > if n <=3D 1: >=20 > > return 1 >=20 > > return fib_memoized(n-1) + fib_memoized(n-2) >=20 > > >=20 > > >=20 > > The second fibonacci looks exactly the same but while the first is >=20 > > very slow and would generate a stack overflow the second doesn't.. >=20 >=20 >=20 > Trouble is, you're starting with a pretty poor algorithm. It's easy to >=20 > improve on what's poor. Memoization can still help, but I would start >=20 > with a better algorithm, such as: >=20 >=20 >=20 > def fib(n): >=20 > if n<=3D1: return 1 >=20 > a,b=3D1,1 >=20 > for i in range(1,n,2): >=20 > a+=3Db >=20 > b+=3Da >=20 > return b if n%2 else a >=20 >=20 >=20 > def fib(n,cache=3D[1,1]): >=20 > if n<=3D1: return 1 >=20 > while len(cache)<=3Dn: >=20 > cache.append(cache[-1] + cache[-2]) >=20 > return cache[n] >=20 >=20 >=20 > Personally, I don't mind (ab)using default arguments for caching, but >=20 > you could do the same sort of thing with a decorator if you prefer. I >=20 > think the non-decorated non-recursive version is clear and efficient >=20 > though. >=20 >=20 >=20 > ChrisA Uhn, the decorator part is good for wrapping functions in python. For example a decorator can be used to add a layor of some message handlings of those plain functions to become iterators which could be used as call back functions in a more elegant way.