You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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=Noneclosed_at=<Date2017-08-20.15:37:46.876>created_at=<Date2017-08-20.05:14:06.200>labels= ['type-feature']
title='Add lazy evaluation support for dict.setdefault()'updated_at=<Date2017-08-20.15:37:46.875>user='https://github.com/JimDennis'
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).
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:
ifkeynotinmydict:
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.
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.
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: