[New-bugs-announce] [issue31240] Add lazy evaluation support for dict.setdefault()

Jim Dennis report at bugs.python.org
Sun Aug 20 01:14:06 EDT 2017

New submission from Jim Dennis:

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).

messages: 300596
nosy: jimd
priority: normal
severity: normal
status: open
title: Add lazy evaluation support for dict.setdefault()
type: enhancement

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list