Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'method,': 0.07; 'subject:Question': 0.07; 'val': 0.07; 'collections': 0.09; 'dict': 0.09; 'it;': 0.09; 'method:': 0.09; 'subclass': 0.09; 'def': 0.10; 'sat,': 0.15; '9:13': 0.16; 'defaultdict': 0.16; 'for,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'in-line': 0.16; 'inclined': 0.16; 'parameter,': 0.16; 'subclassing': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'feb': 0.19; 'skip:v 30': 0.20; 'import': 0.21; 'skip:_ 20': 0.22; 'work,': 0.22; "i'd": 0.22; 'class.': 0.23; 'pass': 0.25; 'header :In-Reply-To:1': 0.25; 'supported': 0.26; 'replace': 0.27; 'message-id:@mail.gmail.com': 0.27; 'arguments.': 0.29; 'class': 0.29; 'sense': 0.31; 'code': 0.31; 'could': 0.32; 'anyone': 0.33; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'project': 0.34; 'involving': 0.35; 'pm,': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'expensive': 0.36; 'skip:m 40': 0.36; 'does': 0.37; 'skip:v 20': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'several': 0.39; 'to:addr:python.org': 0.39; 'called': 0.39; 'your': 0.60; 'provide': 0.62; 'fact,': 0.69; 'frank': 0.75; '2013': 0.84; 'does!': 0.84; 'abc': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=IGA+QdTRMnTu9Zf8aLZmjYkqeeSC4zC6/L5IGG9ADq8=; b=im2ui+3IONtcmk4qHlyL6vu9Hw99g4EpJpsKNPiIweIF5BVrXyfMSGefrHwGk+OVvs R3mchwI2jOABQ/yHr2Wxr/1sBq7Uu7H70CqxQNIGDp6qnhZ+esqRdK0j3Za5YuJIRHnY Q/t2Tp0l725ONoEtGFETRrmaDg5Hf5l1X0mUY1bCkBbowsBqELNlc7EUzLaJf3LpDOrS Mde3an2VLb4PO3mm2nDbiYvyAFDem+vlZzvDgbUxTzWDkq/omrE7MfsyqzX8XDiPuHuC qpAoH1CqCJhtJCq3pUg/TRxewLHH5F/meU2iGKNYiRj5tEIwLt77Gp6p+UMNZ5RQ7GNu NqRQ== MIME-Version: 1.0 X-Received: by 10.221.10.14 with SMTP id oy14mr6348877vcb.34.1361615359960; Sat, 23 Feb 2013 02:29:19 -0800 (PST) In-Reply-To: References: Date: Sat, 23 Feb 2013 21:29:19 +1100 Subject: Re: Question about defaultdict From: Chris Angelico 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361615362 news.xs4all.nl 6961 [2001:888:2000:d::a6]:47771 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39655 On Sat, Feb 23, 2013 at 9:13 PM, Frank Millman wrote: > I thought I could replace this with - > > from collections import defaultdict > my_cache = defaultdict(fetch_object) > my_obj = my_cache['a'] > > It does not work, because fetch_object() is called without any arguments. A reasonable thing to ask for, but not supported by the default defaultdict. However, the key to defaultdict is the __missing__ method, and you can simply subclass dict and provide that method: class cache(dict): def __missing__(self,key): val=fetch_object(key) self[key]=val return val Alternatively, if you want to pass fetch_object as a parameter, subclass defaultdict: >>> class parameterizing_defaultdict(collections.defaultdict): def __missing__(self,key): value=self.default_factory(key) self[key]=value return value >>> my_cache=parameterizing_defaultdict(fetch_object) >>> my_cache["a"] Expensive operation involving a 1 >>> my_cache["a"] 1 >>> my_cache["a"] 1 >>> my_cache["ab"] Expensive operation involving ab 2 >>> my_cache["abc"] Expensive operation involving abc 3 defaultdict does do some other work, but unless you need it, I'd be inclined to go with just subclassing dict directly; in fact, it may make sense to just in-line the fetch_object code right there in the class. There are several ways to go about it; you know your project better than anyone else does! ChrisA