Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29182

Re: Decorators not worth the effort

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 <rosuav@gmail.com>
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 <CAF_E5JZR+2RvZXaMUAsS8nND2pcC06+om2WP+ncWfY-4Z=aPtg@mail.gmail.com>
References <XnsA0CE7D6F43F18duncanbooth@127.0.0.1> <1017333532.1009581.1347628946603.JavaMail.root@sequans.com> <CAF_E5JY6c+QfYGo9N=0p7HdSa6NX8oL-Hj9gR6=7TayQeOqmoA@mail.gmail.com> <CAPTjJmoSt84L_kpKWSUPLUJ38wDVp8ukHovd1QkFYUuJAnoeRQ@mail.gmail.com> <CAF_E5JZR+2RvZXaMUAsS8nND2pcC06+om2WP+ncWfY-4Z=aPtg@mail.gmail.com>
Date Sat, 15 Sep 2012 03:30:12 +1000
Subject Re: Decorators not worth the effort
From Chris Angelico <rosuav@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.715.1347643815.27098.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Sat, Sep 15, 2012 at 2:15 AM, andrea crotti
<andrea.crotti.0@gmail.com> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Decorators not worth the effort Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-09-14 11:28 +0200
  Re: Decorators not worth the effort Duncan Booth <duncan.booth@invalid.invalid> - 2012-09-14 11:26 +0000
    Re: Decorators not worth the effort andrea crotti <andrea.crotti.0@gmail.com> - 2012-09-14 15:12 +0100
    Re: Decorators not worth the effort Chris Angelico <rosuav@gmail.com> - 2012-09-15 00:41 +1000
      Re: Decorators not worth the effort 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-14 09:37 -0700
      Re: Decorators not worth the effort 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-14 09:37 -0700
    Re: Decorators not worth the effort andrea crotti <andrea.crotti.0@gmail.com> - 2012-09-14 17:15 +0100
    Re: Decorators not worth the effort Chris Angelico <rosuav@gmail.com> - 2012-09-15 03:30 +1000
      Re: Decorators not worth the effort Neil Cerutti <neilc@norwich.edu> - 2012-09-18 13:19 +0000
        Re: Decorators not worth the effort Chris Angelico <rosuav@gmail.com> - 2012-09-18 23:25 +1000
          Re: Decorators not worth the effort 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-18 08:14 -0700
          Re: Decorators not worth the effort 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-18 08:14 -0700
    RE: Decorators not worth the effort "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-09-14 23:14 +0000
  Re: Decorators not worth the effort Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-14 12:01 +0000
    Re: Decorators not worth the effort Tim Chase <python.list@tim.thechases.com> - 2012-09-14 08:06 -0500
      Re: Decorators not worth the effort Steve Howell <showell30@yahoo.com> - 2012-09-14 18:13 -0700
  Re: Decorators not worth the effort Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-09-14 13:49 +0200

csiph-web