This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: factory func of collections.defaultdict should receive the "missing key" as args when called.
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, lyricconch
Priority: normal Keywords:

Created on 2011-07-02 07:21 by lyricconch, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg139624 - (view) Author: HaiYun Yan (lyricconch) Date: 2011-07-02 07:21
for example:

def calc(params):
    """ i am factoring numbers. """
    # an expensive CPU cost function but 
    # passin params and return result are both lightweight

cachedcalc = collections.defaultdict(calc)
result = cachedcalc[0xFFFFFFFFFFFFFFFFFFF0AC0FFF1]
msg139625 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-07-02 08:31
-1. Besides compatibility issues, defaultdict is a dict: it contains data, and is not meant to consume CPU when accessing items. Its "default" function should return initial values, like 0 or an empty list.

I think what you want is "memoization"; a memoized function still looks like a function! there are many implementations for python, one of these is here: http://wiki.python.org/moin/PythonDecoratorLibrary#Memoize
History
Date User Action Args
2022-04-11 14:57:19adminsetgithub: 56682
2011-07-02 08:31:11amaury.forgeotdarcsetstatus: open -> closed

nosy: + amaury.forgeotdarc
messages: + msg139625

resolution: not a bug
2011-07-02 07:21:09lyricconchcreate