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!newsfeed4.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.003 X-Spam-Evidence: '*H*': 0.99; '*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; 'def': 0.10; 'defaultdict': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; '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; '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; 'received:co.za': 0.37; 'received:za': 0.37; 'to:addr:python.org': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'received:41': 0.73; 'frank': 0.75 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Frank Millman Subject: Question about defaultdict Date: Sat, 23 Feb 2013 12:13:10 +0200 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 41-135-111-29.dsl.mweb.co.za User-Agent: Mozilla/5.0 (Windows NT 5.2; rv:14.0) Gecko/20120713 Thunderbird/14.0 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: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361614398 news.xs4all.nl 6925 [2001:888:2000:d::a6]:41207 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39653 Hi all 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? Frank Millman