Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #39659
| 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.000 |
| 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:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'skip:@ 20': 0.16; 'wrote:': 0.17; 'thanks,': 0.18; '>>>': 0.18; 'import': 0.21; 'work,': 0.22; 'example': 0.23; 'header:User-Agent:1': 0.26; 'skip:# 10': 0.27; 'replace': 0.27; 'header:X-Complaints-To:1': 0.28; 'chris': 0.28; '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; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'choose': 0.65; 'frank': 0.75; '100': 0.78; 'overlooked': 0.84 |
| 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 12:02:21 +0100 |
| Organization | None |
| References | <kga4nc$4jm$1@ger.gmane.org> <kga6rk$kc2$1@ger.gmane.org> |
| 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 <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.2338.1361617320.2939.python-list@python.org> (permalink) |
| Lines | 40 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1361617320 news.xs4all.nl 6864 [2001:888:2000:d::a6]:33391 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:39659 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Question about defaultdict Peter Otten <__peter__@web.de> - 2013-02-23 12:02 +0100
csiph-web