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: 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: <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 To: Jean-Michel Pichavant Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org, duncan booth 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: 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 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..