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


Groups > comp.lang.python > #29167

Re: Decorators not worth the effort

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <andrea.crotti.0@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; '*args,': 0.07; 'overflow': 0.07; '**kwargs)': 0.09; '**kwargs):': 0.09; 'received:mail- lpp01m010-f46.google.com': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'subject:not': 0.11; 'stack': 0.15; 'cache:': 0.16; 'decorators': 0.22; 'example': 0.23; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'second': 0.24; 'cc:2**1': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'looks': 0.26; 'message-id:@mail.gmail.com': 0.27; 'skip:_ 10': 0.29; 'received:209.85.215.46': 0.30; 'received:google.com': 0.34; 'received:209.85': 0.35; 'but': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'header:Received:5': 0.40; 'think': 0.40; 'first': 0.61; '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 :cc:content-type; bh=+p7e13wCF/doEuoeBdHmsZXTzb08AS4J/KAZysEwRqA=; b=ql0Z6FAYSjYFMMX47DwYZe7YfSL34m5MtwpcK3ZN1stz0hT9uqs/csttTib7PzurtE iNpOhvRnCsfzaySoDsWK95pzmSvhs54V6xPZ0MNMDzYBfhXhVeq8A8fRHFx6cxhj4zGs 2zhHlfGXrF4fqxYv3ubuvHLv9mNpdKgtVJOWoYfgYV9BNbE5wtCx/w0UX1CBXatIc/+e Wrkoyz/cvDYNB2jQwRt4EvbwWnT2Bin3sDpgznfcVLgQo14SBoQiI0valVXLphTsGwfe zwKUiA7+WSgbH2iTulxKucWsUn+cD/8DgOFwXqYZJAfKd9NIB5Dy5l1ohojO38GDTWLf iESg==
MIME-Version 1.0
In-Reply-To <1017333532.1009581.1347628946603.JavaMail.root@sequans.com>
References <XnsA0CE7D6F43F18duncanbooth@127.0.0.1> <1017333532.1009581.1347628946603.JavaMail.root@sequans.com>
Date Fri, 14 Sep 2012 15:12:26 +0100
Subject Re: Decorators not worth the effort
From andrea crotti <andrea.crotti.0@gmail.com>
To Jean-Michel Pichavant <jeanmichel@sequans.com>
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org, duncan booth <duncan.booth@suttoncourtenay.org.uk>
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.704.1347631954.27098.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1347631954 news.xs4all.nl 6940 [2001:888:2000:d::a6]:40994
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:29167

Show key headers only | View raw


I think one very nice and simple example of how decorators can be used is this:

def memoize(f, cache={}, *args, **kwargs):
    def _memoize(*args, **kwargs):
        key = (args, str(kwargs))
        if not key in cache:
            cache[key] = f(*args, **kwargs)

        return cache[key]

    return _memoize

def fib(n):
    if n <= 1:
        return 1
    return fib(n-1) + fib(n-2)

@memoize
def fib_memoized(n):
    if n <= 1:
        return 1
    return fib_memoized(n-1) + fib_memoized(n-2)


The second fibonacci looks exactly the same but while the first is
very slow and would generate a stack overflow the second doesn't..

I might use this example for the presentation, before explaining what it is..

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