Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39660

Re: Question about defaultdict

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'example:': 0.03; '(python': 0.05; 'subject:Question': 0.07; 'python': 0.09; 'collections': 0.09; 'oh,': 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; ':-)': 0.13; 'defaultdict': 0.16; 'functools': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:@ 20': 0.16; 'wrote:': 0.17; 'thanks,': 0.18; '>>>': 0.18; 'import': 0.21; 'work,': 0.22; 'example': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'skip:# 10': 0.27; 'replace': 0.27; 'header:X -Complaints-To:1': 0.28; 'chris': 0.28; '>>>>': 0.29; 'arguments.': 0.29; 'dictionary': 0.29; 'could': 0.32; 'to:addr :python-list': 0.33; 'skip:d 20': 0.34; 'expected': 0.35; 'received:org': 0.36; 'but': 0.36; 'does': 0.37; 'received:co.za': 0.37; 'received:za': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'choose': 0.65; 'received:41': 0.73; 'frank': 0.75; '100': 0.78; 'otten': 0.84; 'overlooked': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Frank Millman <frank@chagford.com>
Subject Re: Question about defaultdict
Date Sat, 23 Feb 2013 13:54:12 +0200
References <kga4nc$4jm$1@ger.gmane.org> <kga6rk$kc2$1@ger.gmane.org> <kga7ii$rkq$1@ger.gmane.org>
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
In-Reply-To <kga7ii$rkq$1@ger.gmane.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2339.1361620458.2939.python-list@python.org> (permalink)
Lines 47
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1361620458 news.xs4all.nl 6939 [2001:888:2000:d::a6]:42974
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:39660

Show key headers only | View raw


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 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.
>>
>> Thanks, Chris and Peter. Some nice ideas to choose from :-)
>>
>> Frank
>
> Oh, I overlooked that you are using Python 3. For that there's also
> functools.lru_cache. Example:
>
>>>> from functools import lru_cache
>>>> @lru_cache(maxsize=None)
> ... def fetch_object(id):
> ...     print("fetching #{}".format(id))
> ...     return id*id # bogus example
> ...
>>>> fetch_object(42)
> fetching #42
> 1764
>>>> fetch_object(42)
> 1764
>>>> fetch_object(10)
> fetching #10
> 100
>
>

Interesting.

Thanks, Peter

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Question about defaultdict Frank Millman <frank@chagford.com> - 2013-02-23 13:54 +0200

csiph-web