Guido van Rossum wrote:
> IIUC the pattern matching uses either .get(key, <sentinel>) or .__contains__(key) followed by .__getitem__(key). Neither of those will auto-add the item to a defaultdict (and the Mapping protocol supports both). @Brandt: what does your implementation currently do? Do you think we need to specify this in the PEP?
I still prefer under-specifying rather than over-specifying, in order to allow the implementation some flexibility. Basically, "if you've got a funny mapping, it may behave funny".
For example, we currently perform a length check before even getting to this point, which is allowed because the spec isn't very strict. This is a good thing; we already have a section in the PEP that explicitly allows compiler to perform transformations such as C(x) | C(y) -> C(x | y).
If we have to specify it, __contains__ followed by __getitem__ is the way to do it. The current behavior is subtly different (and probably wrong), but I was going to change it today anyways... I had an outstanding TODO for it.
So why not .get(key, <sentinel>)? You can reuse the sentinel, and this way it's a single call instead of two -- e.g. the code in Mapping implements both __contains__() and get() by calling __getitem__() and catching KeyError.