Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'cache': 0.04; 'from:addr:canterbury.ac.nz': 0.09; 'from:addr:greg.ewing': 0.09; 'from:name:greg ewing': 0.09; 'header:In-reply-to:1': 0.09; 'message-id:@canterbury.ac.nz': 0.09; 'sun,': 0.09; 'def': 0.13; 'wrote:': 0.14; 'cache.': 0.16; 'lookup': 0.16; 'received:(new zealand standard time)': 0.16; 'received:132.181': 0.16; 'received:132.181.2': 0.16; 'received:ac.nz': 0.16; 'received:canterbury.ac.nz': 0.16; 'received:conversion- daemon.it.canterbury.ac.nz': 0.16; 'received:it.canterbury.ac.nz': 0.16; 'gregory': 0.23; 'objects': 0.24; 'greg': 0.25; 'mine': 0.25; 'subject: -- ': 0.25; 'looks': 0.28; 'ewing': 0.31; 'does': 0.31; 'to:addr:python-list': 0.32; 'received:192.168.1': 0.34; 'received:192': 0.34; 'header:User-Agent:1': 0.35; 'received:132': 0.35; 'received:192.168': 0.37; 'apr': 0.38; 'to:addr:python.org': 0.39; 'how': 0.39; '2011': 0.62; 'received:nz': 0.64 Date: Mon, 18 Apr 2011 11:09:29 +1200 From: Greg Ewing Subject: Re: Feature suggestion -- return if true In-reply-to: <20110417083347.0acf21e5.darcy@druid.net> To: python-list@python.org MIME-version: 1.0 Content-type: text/plain; charset=UTF-8; format=flowed Content-transfer-encoding: 7bit X-Accept-Language: en-us, en User-Agent: Mozilla Thunderbird 1.0.5 (Macintosh/20050711) References: <8abff237-5ccd-4eb6-85c8-cdc9e87520b7@bl1g2000vbb.googlegroups.com> <90v871FkuaU1@mid.individual.net> <20110417083347.0acf21e5.darcy@druid.net> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 26 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303081774 news.xs4all.nl 81481 [::ffff:82.94.164.166]:47891 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3434 D'Arcy J.M. Cain wrote: > On Sun, 17 Apr 2011 16:21:53 +1200 > Gregory Ewing wrote: > >> def get_from_cache(x): >> y = cache.get(x) >> if not y: >> y = compute_from(x) >> cache[x] = y >> return y > > I prefer not to create and destroy objects needlessly. How does that create objects needlessly? > def get_from_cache(x): > if not x in cache: > cache[x] = compute_from(x) > return cache[x] That looks up the cache *twice* for every access. Mine only does one lookup when the item is already in the cache. -- Greg