Skip to content

factory func of collections.defaultdict should receive the "missing key" as args when called. #56682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lyricconch mannequin opened this issue Jul 2, 2011 · 2 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@lyricconch
Copy link
Mannequin

lyricconch mannequin commented Jul 2, 2011

BPO 12473
Nosy @amauryfa

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2011-07-02.08:31:11.474>
created_at = <Date 2011-07-02.07:21:09.476>
labels = ['invalid', 'type-bug', 'library']
title = 'factory func of collections.defaultdict should receive the "missing key" as args when called.'
updated_at = <Date 2011-07-02.08:31:11.467>
user = 'https://bugs.python.org/lyricconch'

bugs.python.org fields:

activity = <Date 2011-07-02.08:31:11.467>
actor = 'amaury.forgeotdarc'
assignee = 'none'
closed = True
closed_date = <Date 2011-07-02.08:31:11.474>
closer = 'amaury.forgeotdarc'
components = ['Library (Lib)']
creation = <Date 2011-07-02.07:21:09.476>
creator = 'lyricconch'
dependencies = []
files = []
hgrepos = []
issue_num = 12473
keywords = []
message_count = 2.0
messages = ['139624', '139625']
nosy_count = 2.0
nosy_names = ['amaury.forgeotdarc', 'lyricconch']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue12473'
versions = ['Python 2.7']

@lyricconch
Copy link
Mannequin Author

lyricconch mannequin commented Jul 2, 2011

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]

@lyricconch lyricconch mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jul 2, 2011
@amauryfa
Copy link
Contributor

amauryfa commented Jul 2, 2011

-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

@amauryfa amauryfa closed this as completed Jul 2, 2011
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant