Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!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.019 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'cache': 0.04; 'sun,': 0.09; 'this:': 0.11; 'def': 0.13; 'wrote:': 0.14; '425': 0.16; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'header:In-Reply- To:1': 0.22; 'cc:addr:python-list': 0.22; 'gregory': 0.23; 'voting': 0.23; 'objects': 0.24; "what's": 0.24; 'subject: -- ': 0.25; 'looks': 0.28; 'cc:addr:python.org': 0.31; 'ewing': 0.31; 'charset:us-ascii': 0.36; 'apr': 0.38; 'header:Mime-Version:1': 0.39; 'header:Message-Id:1': 0.62; '2011': 0.62; 'received:208': 0.62; 'url:net': 0.62; 'democracy': 0.84 Date: Sun, 17 Apr 2011 08:33:47 -0400 From: "D'Arcy J.M. Cain" To: Gregory Ewing Subject: Re: Feature suggestion -- return if true In-Reply-To: <90v871FkuaU1@mid.individual.net> References: <8abff237-5ccd-4eb6-85c8-cdc9e87520b7@bl1g2000vbb.googlegroups.com> <90v871FkuaU1@mid.individual.net> X-Mailer: Sylpheed 3.1.0beta2 (GTK+ 2.22.0; i686-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: python-list@python.org 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: 22 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303043638 news.xs4all.nl 81478 [::ffff:82.94.164.166]:46265 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3394 On Sun, 17 Apr 2011 16:21:53 +1200 Gregory Ewing wrote: > My idiom for fetching from a cache looks like this: > > 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. def get_from_cache(x): if not x in cache: cache[x] = compute_from(x) return cache[x] -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.