Issue 12473: factory func of collections.defaultdict should receive the "missing key" as args when called. (original) (raw)

Issue12473

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:19 admin set github: 56682
2011-07-02 08:31:11 amaury.forgeotdarc set status: open -> closednosy: + amaury.forgeotdarcmessages: + resolution: not a bug
2011-07-02 07:21:09 lyricconch create