[Bar Harel]:
I was just about to fix dict's get() to call __missing__ if a key doesn't
exist (before returning the default value) but realized that although small,
that patch can cause future issues.  Right now there's an inconsistency:

The reason for this inconsistency is because the Mapping abc and dict
behave differently.  Dict's get doesn't call __getitem__ which causes
the call not to route to __missing__.  MutableMapping's get calls __getitem__,
which UserDict implements as a check to __missing__ as well.

Should get() call __missing__?


[Guido van Rossum]
I don't think __missing__ should be called by get() -- get() already has a
way to deal with missing keys, and making it use two different mechanisms
would be weird (e.g. if get() calls __missing__, is the default value ever
used?).


[Ethan Furman]
It could be if __missing__ refused to return a value for some particular keys.

However, I don't think get() should call __missing__.


[Bar Harel]
As for get(). Current implementation of UserDict returns the default value
for get() if __missing__() raises KeyError.  Which sounds reasonable to me.
After all, you would logically expect __missing__ to be called for get()
as you tried to get a non-existing value.


[Ethan Furman]
I disagree.  My understanding of the purpose behind get() is to get a value
or return the default specified in the get() call, not to create a new key
(which the default __missing__ does).

--
~Ethan~
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/M2K2BTDGFYUMQFRBHPY5IFCDYFXDMUET/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to