Skip to content
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

Add lazy evaluation support for dict.setdefault() #75423

Closed
JimDennis mannequin opened this issue Aug 20, 2017 · 3 comments
Closed

Add lazy evaluation support for dict.setdefault() #75423

JimDennis mannequin opened this issue Aug 20, 2017 · 3 comments
Labels
type-feature A feature request or enhancement

Comments

@JimDennis
Copy link
Mannequin

JimDennis mannequin commented Aug 20, 2017

BPO 31240
Nosy @rhettinger, @JimDennis, @serhiy-storchaka

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 2017-08-20.15:37:46.876>
created_at = <Date 2017-08-20.05:14:06.200>
labels = ['type-feature']
title = 'Add lazy evaluation support for dict.setdefault()'
updated_at = <Date 2017-08-20.15:37:46.875>
user = 'https://github.com/JimDennis'

bugs.python.org fields:

activity = <Date 2017-08-20.15:37:46.875>
actor = 'serhiy.storchaka'
assignee = 'none'
closed = True
closed_date = <Date 2017-08-20.15:37:46.876>
closer = 'serhiy.storchaka'
components = []
creation = <Date 2017-08-20.05:14:06.200>
creator = 'jimd'
dependencies = []
files = []
hgrepos = []
issue_num = 31240
keywords = []
message_count = 3.0
messages = ['300596', '300598', '300603']
nosy_count = 3.0
nosy_names = ['rhettinger', 'jimd', 'serhiy.storchaka']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue31240'
versions = []

@JimDennis
Copy link
Mannequin Author

JimDennis mannequin commented Aug 20, 2017

Code such as mydict.setdefault('eggs', []) will needlessly incur the cost of instantiating a list even when 'eggs' is already a valid key in mydict. collections.defaultdict will not do this. detecting and automatically calling "callable" and "type" objects (implicit laziness) would break cases where callables and types are used as first class values.

Add keyword argument: lazy? Thus mydict.setdefault('eggs',list, lazy=True) would generate a new list only when necessary while the default would still be to append a reference to list (first class) objects).

@JimDennis JimDennis mannequin added the type-feature A feature request or enhancement label Aug 20, 2017
@serhiy-storchaka
Copy link
Member

The advantage of dict.setdefault() is its atomicity. With lazy evaluation of the value it can't be atomic anymore, and can be replaced with the following code:

    if key not in mydict:
        mydict[key] = value

I'm -1 for this change. It increases complexity (both semantical and implementational) of dict.setdefault() and doesn't have significant benefit.

@rhettinger
Copy link
Contributor

-1 from me as well.

The need has already been fulfilled in other ways (defaultdict and __missing__). I don't think we need yet another way to do it.

Also, I suspect that the benefit of a lazy instantiation would be more than offset by cost of passing in the arguments (global variable lookup for the list builtin, a load constant for True, and the keyword argument processing).

Lastly, I don't like making the signature more complex or further breaking the already loose symmetry with dict.get() and dict.pop(). Likewise, this isn't parallel with other tools that take a default argument such as min(), max(), getattr(), etc.

@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
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants