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!newsfeed3.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(python': 0.05; 'subject:Question': 0.07; 'collections': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subclass': 0.09; 'def': 0.10; 'defaultdict': 0.16; 'key):': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'self[key]': 0.16; 'wrote:': 0.17; 'import': 0.21; 'work,': 0.22; 'work.': 0.23; 'header:User- Agent:1': 0.26; 'replace': 0.27; 'header:X-Complaints-To:1': 0.28; 'arguments.': 0.29; 'dictionary': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'could': 0.32; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; 'problem,': 0.35; 'expected': 0.35; 'received:org': 0.36; 'but': 0.36; 'expensive': 0.36; 'does': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'frank': 0.75 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Question about defaultdict Date: Sat, 23 Feb 2013 11:34:21 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084be59.dip.t-dialin.net User-Agent: KNode/4.7.3 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361615641 news.xs4all.nl 6885 [2001:888:2000:d::a6]:50759 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39656 Frank Millman wrote: > I use a dictionary as a cache, and I thought that I could replace it > with collections.defaultdict, but it does not work the way I expected > (python 3.3.0). > > my_cache = {} > def get_object(obj_id): > if obj_id not in my_cache: > my_object = fetch_object(obj_id) # expensive operation > my_cache[obj_id] = my_object > return my_cache[obj_id] > my_obj = get_object('a') > > 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. > > It is not a problem, but it would be neat if I could get it to work. Am > I missing anything? You can subclass the ordinary dict: class Cache(dict): def __missing__(self, key): result = self[key] = fetch_object(key) return result _cache = Cache() def get_object(object_id): return _cache[object_id]