Question about defaultdict

2013-02-23 Thread Frank Millman
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

Re: Question about defaultdict

2013-02-23 Thread Chris Angelico
On Sat, Feb 23, 2013 at 9:13 PM, Frank Millman fr...@chagford.com 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

Re: Question about defaultdict

2013-02-23 Thread Peter Otten
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)

Re: Question about defaultdict

2013-02-23 Thread Frank Millman
On 23/02/2013 12:13, Frank Millman wrote: 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). [...] from collections import defaultdict my_cache = defaultdict(fetch_object) my_obj =

Re: Question about defaultdict

2013-02-23 Thread Peter Otten
Frank Millman wrote: On 23/02/2013 12:13, Frank Millman wrote: 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). [...] from collections import defaultdict my_cache =

Re: Question about defaultdict

2013-02-23 Thread Frank Millman
On 23/02/2013 13:02, Peter Otten wrote: Frank Millman wrote: On 23/02/2013 12:13, Frank Millman wrote: 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). [...] from