[Python-Dev] Proposal: defaultdict
"Martin v. Löwis"
martin at v.loewis.de
Sun Feb 19 07:59:58 CET 2006
Raymond Hettinger wrote:
>> If you have a default value, you cannot ultimately del a key. This
>> sequence is *not* a basic mapping invariant.
> You believe that key deletion is not basic to mappings?
No, not in the sense that the key will go away through deletion.
I view a mapping as a modifiable partial function. There is some
initial key/value association (in a classic mapping, it is initially
empty), and then there are modifications. Key deletion means to
reset the key to the initial association.
> Of course, the __del__ followed __contains__ sequence is not the only
> invariant that is thrown-off. There are plenty of examples. Here's one
> that is absolutely basic to the method's contract:
> k, v = dd.popitem()
> assert k not in dd
> Any code that was expecting a dictionary and uses popitem() as a means
> of looping over and consuming entries will fail.
Well, code that loops over a dictionary using popitem typically
terminates when the dictionary becomes false (or its length becomes
zero). That code wouldn't be affected by the behaviour of "in".
> No one should kid themselves that a default dictionary is a drop-in
> substitute. Much of the dict's API has an ambiguous meaning when applied
> to defaultdicts.
Right. But it is only ambiguous until specified. Of course, in the face
of ambiguity, refuse the temptation to guess.
> If all keys are in-theory predefined, what is the meaning of len(dd)?
Taking my definition from the beginning of the message, it is the number
of keys that have been modified from the initial mapping.
> Should dd.items() include any entries where the value is equal to the
> default or should the collection never store those?
It should include all modified items, and none of the unmodified ones.
Explicitly assigning the default value still makes the entry modified;
you need to del it to set it back to "unmodified".
> If the former, then
> how do you access the entries without looping over the whole contents?
Not sure I understand the question. You use d[k] to access an entry.
More information about the Python-Dev