Message 85938 - Python tracker (original) (raw)

The missing key reports the default object, but doesn't set that key
or value object in the dict.

That's just not true. Re-read the line in the doc string where it puts the default value into the dict, under the key being accessed.

For example, to represent a graph structure, it is helpful to use a
dict-of-dicts.

No problem:

py> from collections import defaultdict py> weights=defaultdict(dict) py> weights['a']['b']=17 py> weights['a']['c']=5 py> weights['a'] {'c': 5, 'b': 17}

See? It remembered the default value, and the value for the 'a' key got changed and can now be modified. The whole point of defaultdict was to replace setdefault, so that setdefault can eventually be eliminated, see

http://mail.python.org/pipermail/python-dev/2006-February/061169.html http://mail.python.org/pipermail/python-dev/2006-February/061261.html

Apparently, the plan for removing setdefault is now forgotten. Perhaps this would be a good time to revive it, with a proper deprecation procedure. If it is to be removed, I'm -1 for "fixing" anything about it.